It all started by wondering whether ++i was better than i++ if the return value wasn't being used...

After some searching, public opinion seems to be that ++i is better with the argument being that in machine code, the memory location is simply incremented and the new value is returned, whereas i++ must apparently store the existing value, increment the original memory location and return the stored value. This seems to make sense! As a twist, apparently many compilers make the whole question rather pointless by automatically generating the machine code for ++i even if i++ was entered if the return value is not used!

To make life even more interesting, during the search I found a discussion stating that i+=1 was WAY better than both! Being an inquisitive soul, I decided I should find out for myself - Time for some testing with the Arduino development environment for an ATmega168 micro-controller!

Code: for (long i = 0; i < 999,999,999; <increment method>) x ^= 1;

    • ++i..... 1,762,509 ms
    • i++..... 1,762,511 ms
    • i+=1..... 1,762,509 ms

So it looks like I can use whichever way I feel like as they all produce much the same results!

Now, in the interest of thoroughness, I suppose I'd better try decrementing...

Code: for (long i = 999,999,999; i > 0; <decrement method>) x ^= 1;

    • --i..... 881,255 ms
    • i--..... 881,254 ms
    • i-=1..... 881,254 ms

I was not expecting that!!! Decrementing takes half the time! Now THAT is something to be aware of!

NOTE: The result is of course only really valid for an ATmega168 programmed through the Arduino development environment version that I tested with! Time to store the code away to repeat the test in future versions or other development environments!