mirror of
https://github.com/boostorg/lambda.git
synced 2026-01-22 17:22:48 +00:00
170 lines
13 KiB
HTML
170 lines
13 KiB
HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
||
<html><head><meta content="text/html; charset=ISO-8859-1" http-equiv="Content-Type"><title>7. Practical considerations</title><meta name="generator" content="DocBook XSL Stylesheets V1.48"><link rel="home" href="index.html" title="
|
||
C++ BOOST
|
||
|
||
The Boost Lambda Library"><link rel="up" href="index.html" title="
|
||
C++ BOOST
|
||
|
||
The Boost Lambda Library"><link rel="previous" href="ar01s06.html" title="6. Extending return type deduction system"><link rel="next" href="ar01s08.html" title="8. Relation to other Boost libraries"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">7. Practical considerations</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ar01s06.html">Prev</a> </td><th width="60%" align="center"> </th><td width="20%" align="right"> <a accesskey="n" href="ar01s08.html">Next</a></td></tr></table><hr></div><div class="section"><div class="titlepage"><div><h2 class="title" style="clear: both"><a name="id2807507"></a>7. Practical considerations</h2></div></div><div class="section"><div class="titlepage"><div><h3 class="title"><a name="id2807513"></a>7.1. Performance</h3></div></div><p>In theory, all overhead of using STL algorithms and lambda functors
|
||
compared to hand written loops can be optimized away, just as the overhead
|
||
from standard STL function objects and binders can.
|
||
|
||
Depending on the compiler, this can also be true in practice.
|
||
We ran two tests with the GCC 3.0.4 compiler on 1.5 GHz Intel Pentium 4.
|
||
The optimization flag -03 was used.
|
||
</p><p>
|
||
In the first test we compared lambda functors against explicitly written
|
||
function objects.
|
||
We used both of these styles to define unary functions which multiply the
|
||
argument repeatedly by itself.
|
||
We started with the identity function, going up to
|
||
x<sup>5</sup>.
|
||
The expressions were called inside a <tt>std::transform</tt> loop,
|
||
reading the argument from one <tt>std::vector<int></tt>
|
||
and placing the result into another.
|
||
The length of the vectors was 100 elements.
|
||
The running times are listed in
|
||
<a href="ar01s07.html#table:increasing_arithmetic_test" title="Table 3. Test 1. CPU time of expressions with integer multiplication written as a lambda expression and as a traditional hand-coded function object class.
|
||
The running times are expressed in arbitrary units.">Table 3</a>.
|
||
|
||
We can observe that there is no significant difference between the
|
||
two approaches.
|
||
</p><p>
|
||
In the second test we again used <tt>std::transform</tt> to
|
||
perform an operation to each element in a 100-element long vector.
|
||
This time the element type of the vectors was <tt>double</tt>
|
||
and we started with very simple arithmetic expressions and moved to
|
||
more complex ones.
|
||
The running times are listed in <a href="ar01s07.html#table:ll_vs_stl_test" title="Table 4. Test 2. CPU time of arithmetic expressions written as lambda
|
||
expressions, as classic STL unnamed functions (using compose2, bind1st etc.) and as traditional hand-coded function object classes.
|
||
Using BLL terminology,
|
||
a and b are bound arguments in the expressions, and x is open.
|
||
All variables were of types double.
|
||
The running times are expressed in arbitrary units.">Table 4</a>.
|
||
|
||
Here, we also included classic STL style unnamed functions into tests.
|
||
We do not show these expressions, as they get rather complex.
|
||
For example, the
|
||
last expression in <a href="ar01s07.html#table:ll_vs_stl_test" title="Table 4. Test 2. CPU time of arithmetic expressions written as lambda
|
||
expressions, as classic STL unnamed functions (using compose2, bind1st etc.) and as traditional hand-coded function object classes.
|
||
Using BLL terminology,
|
||
a and b are bound arguments in the expressions, and x is open.
|
||
All variables were of types double.
|
||
The running times are expressed in arbitrary units.">Table 4</a> written with
|
||
classic STL tools contains 7 calls to <tt>compose2</tt>,
|
||
8 calls to <tt>bind1st</tt>
|
||
and altogether 14 constructor invocations for creating
|
||
<tt>multiplies</tt>, <tt>minus</tt>
|
||
and <tt>plus</tt> objects.
|
||
|
||
In this test the BLL expressions are a little slower (roughly 10% on average,
|
||
less than 14% in all cases)
|
||
than the corresponding hand-written function objects.
|
||
The performance hit is a bit greater with classic STL expressions,
|
||
up to 27% for the simplest expressios.
|
||
</p><p>
|
||
The tests suggest that the BLL does not introduce a loss of performance
|
||
compared to STL function objects.
|
||
With a reasonable optimizing compiler, one should expect the performance characteristics be comparable to using classic STL.
|
||
Moreover, with simple expressions the performance can be expected to be close
|
||
to that of explicitly written function objects.
|
||
|
||
|
||
|
||
Note however, that evaluating a lambda functor consist of a sequence of calls to small functions that are declared inline.
|
||
If the compiler fails to actually expand these functions inline,
|
||
the performance can suffer.
|
||
The running time can more than double if this happens.
|
||
Although the above tests do not include such an expression, we have experiensed
|
||
this for some seemingly simple expressions.
|
||
|
||
|
||
<div class="table"><p><a name="table:increasing_arithmetic_test"></a><b>Table 3. Test 1. CPU time of expressions with integer multiplication written as a lambda expression and as a traditional hand-coded function object class.
|
||
The running times are expressed in arbitrary units.</b></p><table summary="Test 1. CPU time of expressions with integer multiplication written as a lambda expression and as a traditional hand-coded function object class.
|
||
The running times are expressed in arbitrary units." border="1"><colgroup><col><col><col></colgroup><thead><tr><th>expression</th><th>lambda expression</th><th>hand-coded function object</th></tr></thead><tbody><tr><td>x</td><td>240</td><td>230</td></tr><tr><td>x*x</td><td>340</td><td>350</td></tr><tr><td>x*x*x</td><td>770</td><td>760</td></tr><tr><td>x*x*x*x</td><td>1180</td><td>1210</td></tr><tr><td>x*x*x*x*x</td><td>1950</td><td>1910</td></tr></tbody></table></div>
|
||
</p><p>
|
||
<div class="table"><p><a name="table:ll_vs_stl_test"></a><b>Table 4. Test 2. CPU time of arithmetic expressions written as lambda
|
||
expressions, as classic STL unnamed functions (using <tt>compose2</tt>, <tt>bind1st</tt> etc.) and as traditional hand-coded function object classes.
|
||
Using BLL terminology,
|
||
<tt>a</tt> and <tt>b</tt> are bound arguments in the expressions, and <tt>x</tt> is open.
|
||
All variables were of types <tt>double</tt>.
|
||
The running times are expressed in arbitrary units.</b></p><table summary="Test 2. CPU time of arithmetic expressions written as lambda
|
||
expressions, as classic STL unnamed functions (using compose2, bind1st etc.) and as traditional hand-coded function object classes.
|
||
Using BLL terminology,
|
||
a and b are bound arguments in the expressions, and x is open.
|
||
All variables were of types double.
|
||
The running times are expressed in arbitrary units." border="1"><colgroup><col><col><col><col></colgroup><thead><tr><th>expression</th><th>lambda expression</th><th>classic STL expression</th><th>hand-coded function object</th></tr></thead><tbody><tr><td>ax</td><td>330</td><td>370</td><td>290</td></tr><tr><td>-ax</td><td>350</td><td>370</td><td>310</td></tr><tr><td>ax-(a+x)</td><td>470</td><td>500</td><td>420</td></tr><tr><td>(ax-(a+x))(a+x)</td><td>620</td><td>670</td><td>600</td></tr><tr><td>((ax) - (a+x))(bx - (b+x))(ax - (b+x))(bx - (a+x))</td><td>1660</td><td>1660</td><td>1460</td></tr></tbody></table></div>
|
||
</p><p>Some additional performance testing with an earlier version of the
|
||
library is described
|
||
[<a href="bi01.html#cit:jarvi:00" title="[Jär00]">Jär00</a>].
|
||
</p></div><div class="section"><div class="titlepage"><div><h3 class="title"><a name="id2808006"></a>7.2. About compiling</h3></div></div><p>The BLL uses templates rather heavily, performing numerous recursive instantiations of the same templates.
|
||
This has (at least) three implications:
|
||
<div class="itemizedlist"><ul type="disc"><li><p>
|
||
While it is possible to write incredibly complex lambda expressions, it probably isn't a good idea.
|
||
Compiling such expressions may end up requiring a lot of memory
|
||
at compile time, and being slow to compile.
|
||
</p></li><li><p>
|
||
The types of lambda functors that result from even the simplest lambda expressions are cryptic.
|
||
Usually the programmer doesn't need to deal with the lambda functor the 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.
|
||
This can make the error messages very long and difficult to interpret, particularly if the compiler outputs the whole chain of template instantiations.
|
||
</p></li><li><p>
|
||
The C++ Standard suggests a template nesting level of 17 to help detect infinite recursion.
|
||
Complex lambda templates can easily exceed this limit.
|
||
Most compilers allow a greater number of nested templates, but commonly require the limit explicitly increased with a command line argument.
|
||
</p></li></ul></div></p></div><div class="section"><div class="titlepage"><div><h3 class="title"><a name="id2808067"></a>7.3. Portability</h3></div></div><p>
|
||
The BLL works with the following compilers, that is, the compilers are capable of compiling the test cases that are included with the BLL:
|
||
|
||
<div class="itemizedlist"><ul type="disc"><li>GCC 3.0.4
|
||
</li><li>KCC 4.0f with EDG 2.43.1
|
||
</li><li>GCC 2.96 (fails with one test case, the <tt>exception_test.cpp</tt> results in an internal compiler error.
|
||
)
|
||
|
||
</li></ul></div>
|
||
</p><div class="section"><div class="titlepage"><div><h4 class="title"><a name="id2808107"></a>7.3.1. Test coverage</h4></div></div><p>The following list describes the test files included and the features that each file covers:
|
||
|
||
<div class="itemizedlist"><ul type="disc"><li><p>
|
||
<tt>bind_tests_simple.cpp</tt> : Bind expressions of different arities and types of target functions: function pointers, function objects and member functions.
|
||
Function composition with bind expressions.</p></li><li><p><tt>bind_tests_simple_function_references.cpp</tt> :
|
||
Repeats all tests from <tt>bind_tests_simple.cpp</tt> where the target function is a function pointer, but uses function references instead.
|
||
</p></li><li><p><tt>bind_tests_advanced.cpp</tt> : Contains tests for nested bind expressions, <tt>unlambda</tt>, <tt>protect</tt>, <tt>const_parameters</tt> and <tt>break_const</tt>.
|
||
Tests passing lambda functors as actual arguments to other lambda functors, currying, and using the <tt>sig</tt> template to specify the return type of a function object.
|
||
</p></li><li><p>
|
||
<tt>operator_tests_simple.cpp</tt> :
|
||
Tests using all operators that are overloaded for lambda expressions, that is, unary and binary arithmetic,
|
||
bitwise,
|
||
comparison,
|
||
logical,
|
||
increment and decrement,
|
||
compound,
|
||
assignment,
|
||
subscrict,
|
||
address of,
|
||
dereference, and comma operators.
|
||
The streaming nature of shift operators is tested, as well as pointer arithmetic with plus and minus operators.
|
||
</p></li><li><p><tt>member_pointer_test.cpp</tt> : The pointer to member operator is complex enough to warrant a separate test file.
|
||
</p></li><li><p>
|
||
<tt>control_structures.cpp</tt> :
|
||
Tests for the looping and if constructs.
|
||
</p></li><li><p>
|
||
<tt>switch_construct.cpp</tt> :
|
||
Includes tests for all supported arities of the switch statement, both with and without the default case.
|
||
</p></li><li><p>
|
||
<tt>exception_test.cpp</tt> :
|
||
Includes tests for throwing exceptions and for try/catch constructs with varying number of catch blocks.
|
||
</p></li><li><p>
|
||
<tt>constructor_tests.cpp</tt> :
|
||
Contains tests for <tt>constructor</tt>, <tt>destructor</tt>, <tt>new_ptr</tt>, <tt>delete_ptr</tt>, <tt>new_array</tt> and <tt>delete_array</tt>.
|
||
</p></li><li><p>
|
||
<tt>cast_test.cpp</tt> : Tests for the four cast expressions, as well as <tt>typeid</tt> and <tt>sizeof</tt>.
|
||
</p></li><li><p>
|
||
<tt>extending_return_type_traits.cpp</tt> : Tests extending the return type deduction system for user defined types.
|
||
Contains several user defined operators and the corresponding specializations for the return type deduction templates.
|
||
</p></li><li><p>
|
||
<tt>is_instance_of_test.cpp</tt> : Includes tests for an internally used traits template, which can detect whether a given type is an instance of a certain template or not.
|
||
</p></li><li><p>
|
||
<tt>bll_and_function.cpp</tt> :
|
||
Contains tests for using <tt>boost::function</tt> together with lambda functors.
|
||
</p></li></ul></div>
|
||
|
||
</p></div></div></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ar01s06.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="index.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="ar01s08.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">6. Extending return type deduction system </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 8. Relation to other Boost libraries</td></tr></table></div></body></html>
|