#include <iostream>
#include <ctime>
#define TIME(t) {std::cout << ((double)(clock() - (t)) / CLOCKS_PER_SEC);}
volatile long int limit = 10000000000;
void l2(int& a) {a++;}
void f(int& a)
{
auto l1 = [&a]()
{
a++;
};
clock_t clk = clock();
for(int i=0;i<limit;i++)
{
l1();
}
TIME(clk) // 4.07 s
a=5;
clk = clock();
for(int i=0;i<limit;i++)
{
l2(a);
}
TIME(clk) // 4.32 s
}
int main()
{
int a = 5;
f(a);
return 0;
}
Why is calling a lambda function faster?
Using gcc 4.8 with O3