4 ms·
Well the obvious answer would be, eliminate the loop, which is what any compiler optimizer will do ;) But let's assume that the operation is not quite so trivi
by dom0 10y ago
Well the obvious answer would be, eliminate the loop, which is what any compiler optimizer will do ;)
But let's assume that the operation is not quite so trivial like here, then this structure would be a prime example where each loop operation is independent from each other, so you can sum a vector in parallel (=SIMD) and then sum the vector once in the end.
Also, since we're obviously not using any compiler optimizations, you can unroll the loop, which reduces per-iteration loop overhead (the conditional jump) and in such a simple case as here is bound to give a nice boost.
- p1esk 10y agoWhen you say "eliminate the loop", do you mean loop unrolling? They are compiling with -O2, I'm not sure if that does loop unrolling. If it does, how much unrolling does it do, exactly? I can't imagine it would construct a block of code with a billion add instructions.
- corndoge 10y agos += NUMBER or s = NUMBER
- danellis 10y agoEliminating the loop means the compiler looks at the code and says "the value computed in this loop is never actually used, so might as well just get rid of that code altogether".
- DannyBee 10y agoFirst, it's unused, so it'll just kill it. If it wasn't, most compilers can compute sum reductions, etc, pretty well. Anything that is easily expressed as a set of simple recurrences that can be solved, it'll solve.