我只是尝试比较 C++11 中 lambda 表达式的性能,所以我做了测试——计算 double
值 vector 中元素的总和。这是实现:
#include <vector>
#include <algorithm>
#include <iostream>
#include <ctime>
#define LOG(x) { std::cout << #x << " = " << (x) << "\n"; }
#define TIME(t) { std::cout << ((double)(clock() - (t)) / CLOCKS_PER_SEC) << " s\n"; }
double sum(const std::vector<double>& v)
{
double s = 0.0;
for (auto i = v.cbegin(); i != v.cend(); ++i)
s += *i;
return s;
}
int main()
{
const size_t MAX = 1; // number of tests
const size_t SIZE = 100000000; // length of the vector
std::vector<double> v(SIZE, 1.0);
double out;
clock_t clk;
std::cout << "iterator\n";
clk = clock();
out = 0.0;
for (size_t i = 0; i < MAX; ++i)
out += sum(v);
TIME(clk)
LOG(out)
std::cout << "\nlambda\n";
clk = clock();
out = 0.0;
for (size_t i = 0; i < MAX; ++i)
std::for_each(v.cbegin(), v.cend(), [&](double d) { out += d; });
TIME(clk)
LOG(out)
return 0;
}
这是这个程序的结果(在VS2010 SP1中编译,在Release模式下):
iterator 0.32 s out = 1e+008 lambda 0.326 s out = 1e+008
As one may see, there is practically no difference in performance. However, if I give 10 as the value of MAX
(it means summation will be performed 10 times instead of one), results differ:
iterator 0.287 s out = 1e+009 lambda 2.84 s out = 1e+009
Test for lambda expression took approximately 10 times more time. Why? I thought it may be caused by the fact, that on every iteration new lambda is created, but whet I tried this:
out = 0.0;
auto f = [&](double d) { out += d; };
for (size_t i = 0; i < MAX; ++i)
std::for_each(v.cbegin(), v.cend(), f);
结果没有改变。有人可以向我解释这种行为吗?
最佳答案
事实证明,这不是 lambda 表达式的任何问题,只是编译器在第一种情况下通过缓存 sum()
函数的结果优化了外循环。将第一种情况更改为这种形式后:
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
}
两种情况的时间大致相等,最喜欢的是 lambda。
关于c++ - 为什么 C++ lambda 在多次调用时比普通函数慢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8611756/