Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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.



When 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.


Eliminating 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".


First, 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.


s += NUMBER

or

s = NUMBER




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: