The answer that I will give to this question is rather more mundane than perhaps you are expecting. The fastest of these variants is the one that, wait for it, is timed to run most quickly.
It's entirely plausible that on different architectures you'll find that different variants win.
It's also conceivable that different variants will win depending on what is in the body of the loop.
It's also quite possible that the body of the loop takes sufficient time that the loop itself is negligible in comparison.
In short, it depends. Since only you know what happens inside the body, only you can answer the specific question.
As an aside, if the loop body does not refer to the loop variable, then the compiler re-writes the ascending loop as if it were a descending loop. So there may in fact be only two variants here. Indeed, that might mean that all three variants lead to identical compiled code!
Some advice:
- Never optimise without profiling.
- Never optimise code that is not a bottleneck.
Now, if you want me to take a guess, I predict that for any loop body that is more than a trivial nop
, you'll find it hard to find any measurable difference between these variants.
I also see that you are using a pointer to walk across an array. You might find that if this code is a bottleneck, and if the loop body just handles this array iteration, that using arr[]
indexing is more effective that pointer arithmetic. But again, it depends on many things and you have to profile, and look at the code the compiler produces.