I have heard that counting down is faster than counting up in loops. However, I also heard that accessing memory forwards (in ascending memory address order) is faster than accessing memory in reverse. Like if I have a loop that repeats a line of code N times, counting down might be slightly faster. But if I access an array with the current value of N, and I count down, I would be accessing the memory in a backwards fashion. Would this be slower and possibly negate all performance benefits of counting down in the first place?
Would this:
short array[1024];
for (int i = 0; i < 1024; ++i) {
do_something_with(array[i]);
}
Be faster than this:
short array[1024];
for (int i = 1024; i--;) {
do_something_with(array[i]);
}
I am trying to write the most fastest code on modern machines.
The latter may be faster on some processors, if you can still measure it. The reason is quite unexpected to those who know assembly. On some processors, we can compare the result of the decrement with zero for free; but comparing with 1024 costs one instruction.
The memory access just does not matter to memory hardware; memory hardware does not work in such a way that order dependence of RAM access cares about increasing or decreasing sequences. However, processor features such as preloading memory may have effects that depend on access direction. (Now if it were mapped memory you might be able to observe increasing access is normally faster than decreasing access on rotating head disks; but that too should not exist on SSDs).
Note the “if you can still measure it”; it is getting hard these days; and mostly nobody cares if you can squeeze that tiny bit of performance out of anything but the hottest loops.