1 | <html> |
---|
2 | <head> |
---|
3 | <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> |
---|
4 | <title>Practical considerations</title> |
---|
5 | <link rel="stylesheet" href="../boostbook.css" type="text/css"> |
---|
6 | <meta name="generator" content="DocBook XSL Stylesheets V1.68.1"> |
---|
7 | <link rel="start" href="../index.html" title="The Boost C++ Libraries BoostBook Documentation Subset"> |
---|
8 | <link rel="up" href="../lambda.html" title="Chapter 8. Boost.Lambda"> |
---|
9 | <link rel="prev" href="extending.html" title="Extending return type deduction system"> |
---|
10 | <link rel="next" href="s08.html" title="Relation to other Boost libraries"> |
---|
11 | </head> |
---|
12 | <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> |
---|
13 | <table cellpadding="2" width="100%"> |
---|
14 | <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../boost.png"></td> |
---|
15 | <td align="center"><a href="../../../index.htm">Home</a></td> |
---|
16 | <td align="center"><a href="../../../libs/libraries.htm">Libraries</a></td> |
---|
17 | <td align="center"><a href="../../../people/people.htm">People</a></td> |
---|
18 | <td align="center"><a href="../../../more/faq.htm">FAQ</a></td> |
---|
19 | <td align="center"><a href="../../../more/index.htm">More</a></td> |
---|
20 | </table> |
---|
21 | <hr> |
---|
22 | <div class="spirit-nav"> |
---|
23 | <a accesskey="p" href="extending.html"><img src="../images/prev.png" alt="Prev"></a><a accesskey="u" href="../lambda.html"><img src="../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../images/home.png" alt="Home"></a><a accesskey="n" href="s08.html"><img src="../images/next.png" alt="Next"></a> |
---|
24 | </div> |
---|
25 | <div class="section" lang="en"> |
---|
26 | <div class="titlepage"><div><div><h2 class="title" style="clear: both"> |
---|
27 | <a name="id1248980"></a>Practical considerations</h2></div></div></div> |
---|
28 | <div class="toc"><dl> |
---|
29 | <dt><span class="section"><a href="s07.html#id1248985">Performance</a></span></dt> |
---|
30 | <dt><span class="section"><a href="s07.html#id1249366">About compiling</a></span></dt> |
---|
31 | <dt><span class="section"><a href="s07.html#id1249416">Portability</a></span></dt> |
---|
32 | </dl></div> |
---|
33 | <div class="section" lang="en"> |
---|
34 | <div class="titlepage"><div><div><h3 class="title"> |
---|
35 | <a name="id1248985"></a>Performance</h3></div></div></div> |
---|
36 | <p>In theory, all overhead of using STL algorithms and lambda functors |
---|
37 | compared to hand written loops can be optimized away, just as the overhead |
---|
38 | from standard STL function objects and binders can. |
---|
39 | |
---|
40 | Depending on the compiler, this can also be true in practice. |
---|
41 | We ran two tests with the GCC 3.0.4 compiler on 1.5 GHz Intel Pentium 4. |
---|
42 | The optimization flag -03 was used. |
---|
43 | </p> |
---|
44 | <p> |
---|
45 | In the first test we compared lambda functors against explicitly written |
---|
46 | function objects. |
---|
47 | We used both of these styles to define unary functions which multiply the |
---|
48 | argument repeatedly by itself. |
---|
49 | We started with the identity function, going up to |
---|
50 | x<sup>5</sup>. |
---|
51 | The expressions were called inside a <code class="literal">std::transform</code> loop, |
---|
52 | reading the argument from one <code class="literal">std::vector<int></code> |
---|
53 | and placing the result into another. |
---|
54 | The length of the vectors was 100 elements. |
---|
55 | The running times are listed in |
---|
56 | <a href="s07.html#table:increasing_arithmetic_test" title="Table 8.3. Test 1">Table 8.3, “Test 1”</a>. |
---|
57 | |
---|
58 | We can observe that there is no significant difference between the |
---|
59 | two approaches. |
---|
60 | </p> |
---|
61 | <p> |
---|
62 | In the second test we again used <code class="literal">std::transform</code> to |
---|
63 | perform an operation to each element in a 100-element long vector. |
---|
64 | This time the element type of the vectors was <code class="literal">double</code> |
---|
65 | and we started with very simple arithmetic expressions and moved to |
---|
66 | more complex ones. |
---|
67 | The running times are listed in <a href="s07.html#table:ll_vs_stl_test" title="Table 8.4. Test 2">Table 8.4, “Test 2”</a>. |
---|
68 | |
---|
69 | Here, we also included classic STL style unnamed functions into tests. |
---|
70 | We do not show these expressions, as they get rather complex. |
---|
71 | For example, the |
---|
72 | last expression in <a href="s07.html#table:ll_vs_stl_test" title="Table 8.4. Test 2">Table 8.4, “Test 2”</a> written with |
---|
73 | classic STL tools contains 7 calls to <code class="literal">compose2</code>, |
---|
74 | 8 calls to <code class="literal">bind1st</code> |
---|
75 | and altogether 14 constructor invocations for creating |
---|
76 | <code class="literal">multiplies</code>, <code class="literal">minus</code> |
---|
77 | and <code class="literal">plus</code> objects. |
---|
78 | |
---|
79 | In this test the BLL expressions are a little slower (roughly 10% on average, |
---|
80 | less than 14% in all cases) |
---|
81 | than the corresponding hand-written function objects. |
---|
82 | The performance hit is a bit greater with classic STL expressions, |
---|
83 | up to 27% for the simplest expressios. |
---|
84 | </p> |
---|
85 | <p> |
---|
86 | The tests suggest that the BLL does not introduce a loss of performance |
---|
87 | compared to STL function objects. |
---|
88 | With a reasonable optimizing compiler, one should expect the performance characteristics be comparable to using classic STL. |
---|
89 | Moreover, with simple expressions the performance can be expected to be close |
---|
90 | to that of explicitly written function objects. |
---|
91 | |
---|
92 | |
---|
93 | |
---|
94 | Note however, that evaluating a lambda functor consist of a sequence of calls to small functions that are declared inline. |
---|
95 | If the compiler fails to actually expand these functions inline, |
---|
96 | the performance can suffer. |
---|
97 | The running time can more than double if this happens. |
---|
98 | Although the above tests do not include such an expression, we have experienced |
---|
99 | this for some seemingly simple expressions. |
---|
100 | |
---|
101 | |
---|
102 | </p> |
---|
103 | <div class="table"> |
---|
104 | <a name="table:increasing_arithmetic_test"></a><p class="title"><b>Table 8.3. Test 1</b></p> |
---|
105 | <div class="caption">CPU time of expressions with integer multiplication written as a lambda expression and as a traditional hand-coded function object class. |
---|
106 | The running times are expressed in arbitrary units.</div> |
---|
107 | <table class="table" summary="Test 1"> |
---|
108 | <colgroup> |
---|
109 | <col> |
---|
110 | <col> |
---|
111 | <col> |
---|
112 | </colgroup> |
---|
113 | <thead><tr> |
---|
114 | <th>expression</th> |
---|
115 | <th>lambda expression</th> |
---|
116 | <th>hand-coded function object</th> |
---|
117 | </tr></thead> |
---|
118 | <tbody> |
---|
119 | <tr> |
---|
120 | <td>x</td> |
---|
121 | <td>240</td> |
---|
122 | <td>230</td> |
---|
123 | </tr> |
---|
124 | <tr> |
---|
125 | <td>x*x</td> |
---|
126 | <td>340</td> |
---|
127 | <td>350</td> |
---|
128 | </tr> |
---|
129 | <tr> |
---|
130 | <td>x*x*x</td> |
---|
131 | <td>770</td> |
---|
132 | <td>760</td> |
---|
133 | </tr> |
---|
134 | <tr> |
---|
135 | <td>x*x*x*x</td> |
---|
136 | <td>1180</td> |
---|
137 | <td>1210</td> |
---|
138 | </tr> |
---|
139 | <tr> |
---|
140 | <td>x*x*x*x*x</td> |
---|
141 | <td>1950</td> |
---|
142 | <td>1910</td> |
---|
143 | </tr> |
---|
144 | </tbody> |
---|
145 | </table> |
---|
146 | </div> |
---|
147 | <p> |
---|
148 | </p> |
---|
149 | <p> |
---|
150 | </p> |
---|
151 | <div class="table"> |
---|
152 | <a name="table:ll_vs_stl_test"></a><p class="title"><b>Table 8.4. Test 2</b></p> |
---|
153 | <div class="caption">CPU time of arithmetic expressions written as lambda |
---|
154 | expressions, as classic STL unnamed functions (using <code class="literal">compose2</code>, <code class="literal">bind1st</code> etc.) and as traditional hand-coded function object classes. |
---|
155 | Using BLL terminology, |
---|
156 | <code class="literal">a</code> and <code class="literal">b</code> are bound arguments in the expressions, and <code class="literal">x</code> is open. |
---|
157 | All variables were of types <code class="literal">double</code>. |
---|
158 | The running times are expressed in arbitrary units.</div> |
---|
159 | <table class="table" summary="Test 2"> |
---|
160 | <colgroup> |
---|
161 | <col> |
---|
162 | <col> |
---|
163 | <col> |
---|
164 | <col> |
---|
165 | </colgroup> |
---|
166 | <thead><tr> |
---|
167 | <th>expression</th> |
---|
168 | <th>lambda expression</th> |
---|
169 | <th>classic STL expression</th> |
---|
170 | <th>hand-coded function object</th> |
---|
171 | </tr></thead> |
---|
172 | <tbody> |
---|
173 | <tr> |
---|
174 | <td>ax</td> |
---|
175 | <td>330</td> |
---|
176 | <td>370</td> |
---|
177 | <td>290</td> |
---|
178 | </tr> |
---|
179 | <tr> |
---|
180 | <td>-ax</td> |
---|
181 | <td>350</td> |
---|
182 | <td>370</td> |
---|
183 | <td>310</td> |
---|
184 | </tr> |
---|
185 | <tr> |
---|
186 | <td>ax-(a+x)</td> |
---|
187 | <td>470</td> |
---|
188 | <td>500</td> |
---|
189 | <td>420</td> |
---|
190 | </tr> |
---|
191 | <tr> |
---|
192 | <td>(ax-(a+x))(a+x)</td> |
---|
193 | <td>620</td> |
---|
194 | <td>670</td> |
---|
195 | <td>600</td> |
---|
196 | </tr> |
---|
197 | <tr> |
---|
198 | <td>((ax) - (a+x))(bx - (b+x))(ax - (b+x))(bx - (a+x))</td> |
---|
199 | <td>1660</td> |
---|
200 | <td>1660</td> |
---|
201 | <td>1460</td> |
---|
202 | </tr> |
---|
203 | </tbody> |
---|
204 | </table> |
---|
205 | </div> |
---|
206 | <p> |
---|
207 | </p> |
---|
208 | <p>Some additional performance testing with an earlier version of the |
---|
209 | library is described |
---|
210 | [<a href="../lambda.html#cit:jarvi:00" title="[Jär00]"><span class="abbrev">Jär00</span></a>]. |
---|
211 | </p> |
---|
212 | </div> |
---|
213 | <div class="section" lang="en"> |
---|
214 | <div class="titlepage"><div><div><h3 class="title"> |
---|
215 | <a name="id1249366"></a>About compiling</h3></div></div></div> |
---|
216 | <p>The BLL uses templates rather heavily, performing numerous recursive instantiations of the same templates. |
---|
217 | This has (at least) three implications: |
---|
218 | </p> |
---|
219 | <div class="itemizedlist"><ul type="disc"> |
---|
220 | <li><p> |
---|
221 | While it is possible to write incredibly complex lambda expressions, it probably isn't a good idea. |
---|
222 | Compiling such expressions may end up requiring a lot of memory |
---|
223 | at compile time, and being slow to compile. |
---|
224 | </p></li> |
---|
225 | <li><p> |
---|
226 | The types of lambda functors that result from even the simplest lambda expressions are cryptic. |
---|
227 | Usually the programmer doesn't need to deal with the lambda functor types at all, but in the case of an error in a lambda expression, the compiler usually outputs the types of the lambda functors involved. |
---|
228 | This can make the error messages very long and difficult to interpret, particularly if the compiler outputs the whole chain of template instantiations. |
---|
229 | </p></li> |
---|
230 | <li><p> |
---|
231 | The C++ Standard suggests a template nesting level of 17 to help detect infinite recursion. |
---|
232 | Complex lambda templates can easily exceed this limit. |
---|
233 | Most compilers allow a greater number of nested templates, but commonly require the limit explicitly increased with a command line argument. |
---|
234 | </p></li> |
---|
235 | </ul></div> |
---|
236 | </div> |
---|
237 | <div class="section" lang="en"> |
---|
238 | <div class="titlepage"><div><div><h3 class="title"> |
---|
239 | <a name="id1249416"></a>Portability</h3></div></div></div> |
---|
240 | <div class="toc"><dl><dt><span class="section"><a href="s07.html#id1249449">Test coverage</a></span></dt></dl></div> |
---|
241 | <p> |
---|
242 | The BLL works with the following compilers, that is, the compilers are capable of compiling the test cases that are included with the BLL: |
---|
243 | |
---|
244 | </p> |
---|
245 | <div class="itemizedlist"><ul type="disc"> |
---|
246 | <li>GCC 3.0.4 |
---|
247 | </li> |
---|
248 | <li>KCC 4.0f with EDG 2.43.1 |
---|
249 | </li> |
---|
250 | <li>GCC 2.96 (fails with one test case, the <code class="filename">exception_test.cpp</code> results in an internal compiler error. |
---|
251 | ) |
---|
252 | |
---|
253 | </li> |
---|
254 | </ul></div> |
---|
255 | <p> |
---|
256 | </p> |
---|
257 | <div class="section" lang="en"> |
---|
258 | <div class="titlepage"><div><div><h4 class="title"> |
---|
259 | <a name="id1249449"></a>Test coverage</h4></div></div></div> |
---|
260 | <p>The following list describes the test files included and the features that each file covers: |
---|
261 | |
---|
262 | </p> |
---|
263 | <div class="itemizedlist"><ul type="disc"> |
---|
264 | <li><p> |
---|
265 | <code class="filename">bind_tests_simple.cpp</code> : Bind expressions of different arities and types of target functions: function pointers, function objects and member functions. |
---|
266 | Function composition with bind expressions.</p></li> |
---|
267 | <li><p><code class="filename">bind_tests_simple_function_references.cpp</code> : |
---|
268 | Repeats all tests from <code class="filename">bind_tests_simple.cpp</code> where the target function is a function pointer, but uses function references instead. |
---|
269 | </p></li> |
---|
270 | <li><p><code class="filename">bind_tests_advanced.cpp</code> : Contains tests for nested bind expressions, <code class="literal">unlambda</code>, <code class="literal">protect</code>, <code class="literal">const_parameters</code> and <code class="literal">break_const</code>. |
---|
271 | Tests passing lambda functors as actual arguments to other lambda functors, currying, and using the <code class="literal">sig</code> template to specify the return type of a function object. |
---|
272 | </p></li> |
---|
273 | <li><p> |
---|
274 | <code class="filename">operator_tests_simple.cpp</code> : |
---|
275 | Tests using all operators that are overloaded for lambda expressions, that is, unary and binary arithmetic, |
---|
276 | bitwise, |
---|
277 | comparison, |
---|
278 | logical, |
---|
279 | increment and decrement, |
---|
280 | compound, |
---|
281 | assignment, |
---|
282 | subscrict, |
---|
283 | address of, |
---|
284 | dereference, and comma operators. |
---|
285 | The streaming nature of shift operators is tested, as well as pointer arithmetic with plus and minus operators. |
---|
286 | </p></li> |
---|
287 | <li><p><code class="filename">member_pointer_test.cpp</code> : The pointer to member operator is complex enough to warrant a separate test file. |
---|
288 | </p></li> |
---|
289 | <li><p> |
---|
290 | <code class="filename">control_structures.cpp</code> : |
---|
291 | Tests for the looping and if constructs. |
---|
292 | </p></li> |
---|
293 | <li><p> |
---|
294 | <code class="filename">switch_construct.cpp</code> : |
---|
295 | Includes tests for all supported arities of the switch statement, both with and without the default case. |
---|
296 | </p></li> |
---|
297 | <li><p> |
---|
298 | <code class="filename">exception_test.cpp</code> : |
---|
299 | Includes tests for throwing exceptions and for try/catch constructs with varying number of catch blocks. |
---|
300 | </p></li> |
---|
301 | <li><p> |
---|
302 | <code class="filename">constructor_tests.cpp</code> : |
---|
303 | Contains tests for <code class="literal">constructor</code>, <code class="literal">destructor</code>, <code class="literal">new_ptr</code>, <code class="literal">delete_ptr</code>, <code class="literal">new_array</code> and <code class="literal">delete_array</code>. |
---|
304 | </p></li> |
---|
305 | <li><p> |
---|
306 | <code class="filename">cast_test.cpp</code> : Tests for the four cast expressions, as well as <code class="filename">typeid</code> and <code class="literal">sizeof</code>. |
---|
307 | </p></li> |
---|
308 | <li><p> |
---|
309 | <code class="filename">extending_return_type_traits.cpp</code> : Tests extending the return type deduction system for user defined types. |
---|
310 | Contains several user defined operators and the corresponding specializations for the return type deduction templates. |
---|
311 | </p></li> |
---|
312 | <li><p> |
---|
313 | <code class="filename">is_instance_of_test.cpp</code> : Includes tests for an internally used traits template, which can detect whether a given type is an instance of a certain template or not. |
---|
314 | </p></li> |
---|
315 | <li><p> |
---|
316 | <code class="filename">bll_and_function.cpp</code> : |
---|
317 | Contains tests for using <code class="literal">boost::function</code> together with lambda functors. |
---|
318 | </p></li> |
---|
319 | </ul></div> |
---|
320 | <p> |
---|
321 | |
---|
322 | </p> |
---|
323 | </div> |
---|
324 | </div> |
---|
325 | </div> |
---|
326 | <table width="100%"><tr> |
---|
327 | <td align="left"></td> |
---|
328 | <td align="right"><small>Copyright © 1999-2004 Jaakko Järvi, Gary Powell</small></td> |
---|
329 | </tr></table> |
---|
330 | <hr> |
---|
331 | <div class="spirit-nav"> |
---|
332 | <a accesskey="p" href="extending.html"><img src="../images/prev.png" alt="Prev"></a><a accesskey="u" href="../lambda.html"><img src="../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../images/home.png" alt="Home"></a><a accesskey="n" href="s08.html"><img src="../images/next.png" alt="Next"></a> |
---|
333 | </div> |
---|
334 | </body> |
---|
335 | </html> |
---|