Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_33_1/doc/html/lambda/s07.html @ 12

Last change on this file since 12 was 12, checked in by landauf, 18 years ago

added boost

File size: 13.4 KB
Line 
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.69.1">
7<link rel="start" href="../index.html" title="The Boost C++ Libraries">
8<link rel="up" href="../lambda.html" title="Chapter 6. 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.png (6897 bytes)" 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><h3 class="title">
27<a name="id2712755"></a>Practical considerations</h3></div></div></div>
28<div class="toc"><dl>
29<dt><span class="section"><a href="s07.html#id2712759">Performance</a></span></dt>
30<dt><span class="section"><a href="s07.html#id2713087">About compiling</a></span></dt>
31<dt><span class="section"><a href="s07.html#id2713129">Portability</a></span></dt>
32</dl></div>
33<div class="section" lang="en">
34<div class="titlepage"><div><div><h4 class="title">
35<a name="id2712759"></a>Performance</h4></div></div></div>
36<p>In theory, all overhead of using STL algorithms and lambda functors
37compared to hand written loops can be optimized away, just as the overhead
38from standard STL function objects and binders can.
39
40Depending on the compiler, this can also be true in practice.
41We ran two tests with the GCC 3.0.4 compiler on 1.5 GHz Intel Pentium 4.
42The optimization flag -03 was used.
43</p>
44<p>
45In the first test we compared lambda functors against explicitly written
46function objects.
47We used both of these styles to define unary functions which multiply the
48argument repeatedly by itself.
49We started with the identity function, going up to
50x<sup>5</sup>.
51The expressions were called inside a <code class="literal">std::transform</code> loop,
52reading the argument from one <code class="literal">std::vector&lt;int&gt;</code> 
53and placing the result into another.
54The length of the vectors was 100 elements.
55The running times are listed in
56<a href="s07.html#table:increasing_arithmetic_test" title="Table 6.3. Test 1">Table 6.3, &#8220;Test 1&#8221;</a>.
57
58We can observe that there is no significant difference between the
59two approaches.
60</p>
61<p>
62In the second test we again used <code class="literal">std::transform</code> to
63perform an operation to each element in a 100-element long vector.
64This time the element type of the vectors was <code class="literal">double</code>
65and we started with very simple arithmetic expressions and moved to
66more complex ones.
67The running times are listed in <a href="s07.html#table:ll_vs_stl_test" title="Table 6.4. Test 2">Table 6.4, &#8220;Test 2&#8221;</a>.
68
69Here, we also included classic STL style unnamed functions into tests.
70We do not show these expressions, as they get rather complex.
71For example, the
72last expression in <a href="s07.html#table:ll_vs_stl_test" title="Table 6.4. Test 2">Table 6.4, &#8220;Test 2&#8221;</a> written with
73classic STL tools contains 7 calls to <code class="literal">compose2</code>,
748 calls to <code class="literal">bind1st</code>
75and altogether 14 constructor invocations for creating
76<code class="literal">multiplies</code>, <code class="literal">minus</code> 
77and <code class="literal">plus</code> objects.
78
79In this test the BLL expressions are a little slower (roughly 10% on average,
80less than 14% in all cases)
81than the corresponding hand-written function objects.
82The performance hit is a bit greater with classic STL expressions,
83up to 27% for the simplest expressios.
84</p>
85<p>
86The tests suggest that the BLL does not introduce a loss of performance
87compared to STL function objects. 
88With a reasonable optimizing compiler, one should expect the performance characteristics be comparable to using classic STL.
89Moreover, with simple expressions the performance can be expected to be close
90to that of explicitly written function objects.
91
92
93
94Note however, that evaluating a lambda functor consist of a sequence of calls to small functions that are declared inline.
95If the compiler fails to actually expand these functions inline,
96the performance can suffer.
97The running time can more than double if this happens.
98Although the above tests do not include such an expression, we have experienced
99this 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 6.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.
106The 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<div class="table">
148<a name="table:ll_vs_stl_test"></a><p class="title"><b>Table 6.4. Test 2</b></p>
149<div class="caption">CPU time of arithmetic expressions written as lambda
150expressions, 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.
151Using BLL terminology,
152<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.
153All variables were of types <code class="literal">double</code>.
154The running times are expressed in arbitrary units.</div>
155<table class="table" summary="Test 2">
156<colgroup>
157<col>
158<col>
159<col>
160<col>
161</colgroup>
162<thead><tr>
163<th>expression</th>
164<th>lambda expression</th>
165<th>classic STL expression</th>
166<th>hand-coded function object</th>
167</tr></thead>
168<tbody>
169<tr>
170<td>ax</td>
171<td>330</td>
172<td>370</td>
173<td>290</td>
174</tr>
175<tr>
176<td>-ax</td>
177<td>350</td>
178<td>370</td>
179<td>310</td>
180</tr>
181<tr>
182<td>ax-(a+x)</td>
183<td>470</td>
184<td>500</td>
185<td>420</td>
186</tr>
187<tr>
188<td>(ax-(a+x))(a+x)</td>
189<td>620</td>
190<td>670</td>
191<td>600</td>
192</tr>
193<tr>
194<td>((ax) - (a+x))(bx - (b+x))(ax - (b+x))(bx - (a+x))</td>
195<td>1660</td>
196<td>1660</td>
197<td>1460</td>
198</tr>
199</tbody>
200</table>
201</div>
202<p>Some additional performance testing with an earlier version of the
203library is described
204[<a href="../lambda.html#cit:jarvi:00" title="[Jär00]"><span class="abbrev">Jär00</span></a>].
205</p>
206</div>
207<div class="section" lang="en">
208<div class="titlepage"><div><div><h4 class="title">
209<a name="id2713087"></a>About compiling</h4></div></div></div>
210<p>The BLL uses templates rather heavily, performing numerous recursive instantiations of the same templates.
211This has (at least) three implications:
212</p>
213<div class="itemizedlist"><ul type="disc">
214<li><p>
215While it is possible to write incredibly complex lambda expressions, it probably isn't a good idea.
216Compiling such expressions may end up requiring a lot of memory
217at compile time, and being slow to compile.
218</p></li>
219<li><p>
220The types of lambda functors that result from even the simplest lambda expressions are cryptic.
221Usually 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.
222This can make the error messages very long and difficult to interpret, particularly if the compiler outputs the whole chain of template instantiations.
223</p></li>
224<li><p>
225The C++ Standard suggests a template nesting level of 17 to help detect infinite recursion.
226Complex lambda templates can easily exceed this limit.
227Most compilers allow a greater number of nested templates, but commonly require the limit explicitly increased with a command line argument.
228</p></li>
229</ul></div>
230</div>
231<div class="section" lang="en">
232<div class="titlepage"><div><div><h4 class="title">
233<a name="id2713129"></a>Portability</h4></div></div></div>
234<div class="toc"><dl><dt><span class="section"><a href="s07.html#id2713153">Test coverage</a></span></dt></dl></div>
235<p>
236The BLL works with the following compilers, that is, the compilers are capable of compiling the test cases that are included with the BLL:
237
238      </p>
239<div class="itemizedlist"><ul type="disc">
240<li>GCC 3.0.4
241        </li>
242<li>KCC 4.0f with EDG 2.43.1
243        </li>
244<li>GCC 2.96 (fails with one test case, the <code class="filename">exception_test.cpp</code> results in an internal compiler error.
245)
246
247        </li>
248</ul></div>
249<div class="section" lang="en">
250<div class="titlepage"><div><div><h5 class="title">
251<a name="id2713153"></a>Test coverage</h5></div></div></div>
252<p>The following list describes the test files included and the features that each file covers:
253
254</p>
255<div class="itemizedlist"><ul type="disc">
256<li><p><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.
257Function composition with bind expressions.</p></li>
258<li><p><code class="filename">bind_tests_simple_function_references.cpp</code> :
259Repeats all tests from <code class="filename">bind_tests_simple.cpp</code> where the target function is a function pointer, but uses function references instead.
260</p></li>
261<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>.
262Tests 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.
263</p></li>
264<li><p><code class="filename">operator_tests_simple.cpp</code> :
265Tests using all operators that are overloaded for lambda expressions, that is, unary and binary arithmetic,
266bitwise,
267comparison,
268logical,
269increment and decrement,
270compound,
271assignment,
272subscrict,
273address of,
274dereference, and comma operators.
275The streaming nature of shift operators is tested, as well as pointer arithmetic with plus and minus operators.
276</p></li>
277<li><p><code class="filename">member_pointer_test.cpp</code> : The pointer to member operator is complex enough to warrant a separate test file.
278</p></li>
279<li><p><code class="filename">control_structures.cpp</code> :
280Tests for the looping and if constructs.
281</p></li>
282<li><p><code class="filename">switch_construct.cpp</code> :
283Includes tests for all supported arities of the switch statement, both with and without the default case.
284</p></li>
285<li><p><code class="filename">exception_test.cpp</code> :
286Includes tests for throwing exceptions and for try/catch constructs with varying number of catch blocks.
287</p></li>
288<li><p><code class="filename">constructor_tests.cpp</code> :
289Contains 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>.
290</p></li>
291<li><p><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>.
292</p></li>
293<li><p><code class="filename">extending_return_type_traits.cpp</code> : Tests extending the return type deduction system for user defined types.
294Contains several user defined operators and the corresponding specializations for the return type deduction templates.
295</p></li>
296<li><p><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.
297</p></li>
298<li><p><code class="filename">bll_and_function.cpp</code> :
299Contains tests for using <code class="literal">boost::function</code> together with lambda functors.
300</p></li>
301</ul></div>
302</div>
303</div>
304</div>
305<table width="100%"><tr>
306<td align="left"></td>
307<td align="right"><small>Copyright © 1999-2004 Jaakko Järvi, Gary Powell</small></td>
308</tr></table>
309<hr>
310<div class="spirit-nav">
311<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>
312</div>
313</body>
314</html>
Note: See TracBrowser for help on using the repository browser.