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.
In the first test we compared lambda functors against explicitly written
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
The expressions were called inside a
reading the argument from one
and placing the result into another.
The length of the vectors was 100 elements.
The running times are listed in
Table 8.3, “Test 1”.
We can observe that there is no significant difference between the
In the second test we again used
perform an operation to each element in a 100-element long vector.
This time the element type of the vectors was
and we started with very simple arithmetic expressions and moved to
more complex ones.
The running times are listed in Table 8.4, “Test 2”.
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 Table 8.4, “Test 2” written with
classic STL tools contains 7 calls to
8 calls to
and altogether 14 constructor invocations for creating
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.
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 experienced this for some seemingly simple expressions.
Table 8.3. Test 1
|expression||lambda expression||hand-coded function object|
Table 8.4. Test 2
|expression||lambda expression||classic STL expression||hand-coded function object|
|((ax) - (a+x))(bx - (b+x))(ax - (b+x))(bx - (a+x))||1660||1660||1460|
Some additional performance testing with an earlier version of the library is described [Jär00].
The BLL uses templates rather heavily, performing numerous recursive instantiations of the same templates. This has (at least) three implications:
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.
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 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.
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.
The BLL works with the following compilers, that is, the compilers are capable of compiling the test cases that are included with the BLL:
exception_test.cppresults in an internal compiler error. )
The following list describes the test files included and the features that each file covers:
bind_tests_simple.cpp : Bind expressions of different arities and types of target functions: function pointers, function objects and member functions.
Function composition with bind expressions.
Repeats all tests from
bind_tests_simple.cpp where the target function is a function pointer, but uses function references instead.
bind_tests_advanced.cpp : Contains tests for nested bind expressions,
Tests passing lambda functors as actual arguments to other lambda functors, currying, and using the
sig template to specify the return type of a function object.
Tests using all operators that are overloaded for lambda expressions, that is, unary and binary arithmetic,
increment and decrement,
dereference, and comma operators.
The streaming nature of shift operators is tested, as well as pointer arithmetic with plus and minus operators.
member_pointer_test.cpp : The pointer to member operator is complex enough to warrant a separate test file.
Tests for the looping and if constructs.
Includes tests for all supported arities of the switch statement, both with and without the default case.
Includes tests for throwing exceptions and for try/catch constructs with varying number of catch blocks.
Contains tests for
cast_test.cpp : Tests for the four cast expressions, as well as
extending_return_type_traits.cpp : 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.
is_instance_of_test.cpp : Includes tests for an internally used traits template, which can detect whether a given type is an instance of a certain template or not.
Contains tests for using
boost::function together with lambda functors.
|Copyright © 1999-2004 Jaakko Järvi, Gary Powell|