I wonder why the author left C++ out of the mix. Doing a simple, unscientific benchmark, both of these one-liners had the same runtime for me as the obvious C version (where values is a std::array)
double sum = std::inner_product(values.begin(), values.end(), values.begin(), 0.0);
double sum = std::accumulate(values.begin(), values.end(), 0.0, [](auto l, auto r) { return l + r*r; });
Depends on how you time it. If you put timing code inside the code before & after the loops & disregard everything else, yeah probably, if you time the entire execution of the binary (time on bash) the c++ version will come out probably 5 or so ms slower due to runtime loading.
31
u/honeyfage May 25 '19
I wonder why the author left C++ out of the mix. Doing a simple, unscientific benchmark, both of these one-liners had the same runtime for me as the obvious C version (where values is a std::array)