[12] | 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 |
---|
| 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 6.3. Test 1">Table 6.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 6.4. Test 2">Table 6.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 6.4. Test 2">Table 6.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 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. |
---|
| 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 | <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 |
---|
| 150 | 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. |
---|
| 151 | Using 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. |
---|
| 153 | All variables were of types <code class="literal">double</code>. |
---|
| 154 | The 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 |
---|
| 203 | library 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. |
---|
| 211 | This has (at least) three implications: |
---|
| 212 | </p> |
---|
| 213 | <div class="itemizedlist"><ul type="disc"> |
---|
| 214 | <li><p> |
---|
| 215 | While it is possible to write incredibly complex lambda expressions, it probably isn't a good idea. |
---|
| 216 | Compiling such expressions may end up requiring a lot of memory |
---|
| 217 | at compile time, and being slow to compile. |
---|
| 218 | </p></li> |
---|
| 219 | <li><p> |
---|
| 220 | The types of lambda functors that result from even the simplest lambda expressions are cryptic. |
---|
| 221 | 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. |
---|
| 222 | This 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> |
---|
| 225 | The C++ Standard suggests a template nesting level of 17 to help detect infinite recursion. |
---|
| 226 | Complex lambda templates can easily exceed this limit. |
---|
| 227 | Most 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> |
---|
| 236 | The 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. |
---|
| 257 | Function composition with bind expressions.</p></li> |
---|
| 258 | <li><p><code class="filename">bind_tests_simple_function_references.cpp</code> : |
---|
| 259 | 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. |
---|
| 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>. |
---|
| 262 | 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. |
---|
| 263 | </p></li> |
---|
| 264 | <li><p><code class="filename">operator_tests_simple.cpp</code> : |
---|
| 265 | Tests using all operators that are overloaded for lambda expressions, that is, unary and binary arithmetic, |
---|
| 266 | bitwise, |
---|
| 267 | comparison, |
---|
| 268 | logical, |
---|
| 269 | increment and decrement, |
---|
| 270 | compound, |
---|
| 271 | assignment, |
---|
| 272 | subscrict, |
---|
| 273 | address of, |
---|
| 274 | dereference, and comma operators. |
---|
| 275 | The 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> : |
---|
| 280 | Tests for the looping and if constructs. |
---|
| 281 | </p></li> |
---|
| 282 | <li><p><code class="filename">switch_construct.cpp</code> : |
---|
| 283 | Includes 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> : |
---|
| 286 | Includes 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> : |
---|
| 289 | 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>. |
---|
| 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. |
---|
| 294 | Contains 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> : |
---|
| 299 | Contains 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> |
---|