[英]Why C++ template code runs faster in my lambdas?
I've been doing some simple vector data accumulate tests and wondering why the following code:我一直在做一些简单的矢量数据累积测试,想知道为什么会出现以下代码:
template<typename T>
T accum(typename vector<T>::const_iterator first, typename vector<T>::const_iterator last, T init) {
for(; first != last; ++first)
init += *first;
return init;
}
runs faster than the following code (both in lamda) ?运行速度比以下代码(均在 lamda 中)快?
test("Iterator/direct", table, [](auto& values) {
auto sum {0.0};
for(auto i = values.begin(); i != values.end(); ++i)
sum += *i;
return(sum);
});
In my eyes the algorithm itself should be quite much the same.在我看来,算法本身应该大同小异。
The whole source code (53 lines) can be found in this Gist .整个源代码(53 行)可以在这个 Gist 中找到。
Compile with -O2 option and the execution times will be practically negligible.使用 -O2 选项编译,执行时间几乎可以忽略不计。 Thank you very much for all commenters.非常感谢所有评论者。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.