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

32

u/scatters May 25 '19

Perhaps it's "obvious" that the idiomatic C++ code should have identical behavior to the C version?

Your code is a bit out of date, though; you should be using transform_reduce:

double sum = std::transform_reduce(std::execution::par_unseq, begin(values), end(values),
    0., std::plus<>{}, [](auto x) { return x * x; });

The parallelism gives a significant speed boost: from 26ms down to 11ms on my 4-core laptop.

19

u/MorrisonLevi May 26 '19

Nothing in the article benchmarked parallel algorithms, so I doubt honeyfage would have even considered it, even if they know about transform_reduce.

5

u/scatters May 26 '19

That's the advantage of the execution policy overloads though: they make it super easy in the idiomatic solution to go and investigate the benefit of parallelism.

2

u/[deleted] May 26 '19

The Rust version gets parallelized by changing .iter() to .par_iter().

2

u/warlockface May 27 '19
error[E0599]: no method named `par_iter` found for type `std::vec::Vec<f64>` in the current scope

3

u/[deleted] May 27 '19 edited May 27 '19

Tthe ParallelIterator trait needs to be in scope (playground), one can import it like this:

use rayon::prelude::*;

If you are doing parallelism, this is typically only done once at the top-level of your module / library.

2

u/Tollyx May 27 '19

He forgot to mention that you need the crate called rayon

16

u/arnoo May 26 '19

transform_reduce without parallelism generates the exact same assembly as the idiomatic C code. See https://godbolt.org/z/KRblWv

16

u/Astrokiwi May 25 '19

I'd like to see Fortran here too. This kind of thing is exactly Fortran's niche:

sum_out = sum(values**2)

which is clear, concise, and efficient.

Python with numpy is similar, but a bit slower:

sum = np.sum(values**2)

1

u/Sopel97 May 27 '19

are they both lazy?

1

u/Astrokiwi May 27 '19

You mean in terms of compiling?

1

u/Sopel97 May 27 '19

in terms of execution, so if there is any intermediate array being created or not

1

u/Astrokiwi May 27 '19

Python/numpy creates intermediate arrays, because the precompiled library calls are linked by interpreted Python, but I think Fortran will optimize them out, although that might depend on the compiler and its settings.

1

u/vks_ May 27 '19

NumPy is never lazy. AFAIK, Fortran might optimize intermediate expressions away.

→ More replies (11)