I just have tried to compare performance of lambda expressions in C++11, so I did the test -- compute sum of elements in a vector of double
values. Here is the
It turned out, that this is not any issue with lambda expressions, just the compiler optimized-out the outer loop in the first case by caching the result of the sum()
function. After change the first case to this form:
out = 0.0;
for (size_t i = 0; i < MAX; ++i)
{
out += sum(v);
v[i] = 1.0; // this adds O(1) time and prevents caching
}
both cases timings are approximately equal, with lambda as a favourite.