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) ?
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 .
Compile with -O2 option and the execution times will be practically negligible. Thank you very much for all commenters.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.