I can think of a situation where postfix is slower than prefix increment:
Imagine a processor with register A
is used as accumulator and it's the only register used in many instructions (some small microcontrollers are actually like this).
Now imagine the following program and their translation into a hypothetical assembly:
Prefix increment:
a = ++b + c;
; increment b
LD A, [&b]
INC A
ST A, [&b]
; add with c
ADD A, [&c]
; store in a
ST A, [&a]
Postfix increment:
a = b++ + c;
; load b
LD A, [&b]
; add with c
ADD A, [&c]
; store in a
ST A, [&a]
; increment b
LD A, [&b]
INC A
ST A, [&b]
Note how the value of b
was forced to be reloaded. With prefix increment, the compiler can just increment the value and go ahead with using it, possibly avoid reloading it since the desired value is already in the register after the increment. However, with postfix increment, the compiler has to deal with two values, one the old and one the incremented value which as I show above results in one more memory access.
Of course, if the value of the increment is not used, such as a single i++;
statement, the compiler can (and does) simply generate an increment instruction regardless of postfix or prefix usage.
As a side note, I'd like to mention that an expression in which there is a b++
cannot simply be converted to one with ++b
without any additional effort (for example by adding a - 1
). So comparing the two if they are part of some expression is not really valid. Often, where you use b++
inside an expression you cannot use ++b
, so even if ++b
were potentially more efficient, it would simply be wrong. Exception is of course if the expression is begging for it (for example a = b++ + 1;
which can be changed to a = ++b;
).