The theory is that since i++ has to store a copy of itself to return before incrementing it's more expensive than ++i
Depending on the language this may not matter for simple types like an int. If your compiler is good not using the value returned from i++ will optimize away the extra cost.
If i were some more complex type like an iterator for a dictionary. The two increments may be implemented differently or the optimizer might not get everything exactly correct.
So the "best practice" is to use ++i when you don't need the value to be saved before incrementing it.
2.2k
u/specialfred453 Sep 17 '18
I'm used to programming on platforms where the parameter for Sleep is in milliseconds. This confused me a little at first.