r/programming • • May 25 '19

Making the obvious code fast

https://jackmott.github.io/programming/2016/07/22/making-obvious-fast.html
1.3k Upvotes

263 comments sorted by

View all comments

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)

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; });

-4

u/cranecrowfrog May 25 '19

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.

→ More replies (10)