I know that one of them is efficient. But dont know which one of them.
I suppose it's possible for an implementation to execute one form more
efficiently than the other. You would need to look at the assembler
output for the two cases.
C mandates nothing about the "efficiency" of implementation, therefore
in a given C implementation, either operation may result in anything from
a single increment instruction for a given usage to a call to multiple
nested subroutines (unlikely but possble). And this could change with the
exact usage. - A different implementation may do the reverse, generate
one code sequence for one form, and a completely different code sequence
for another form. Examining the assembly output of a compiler may give you
a hint as to which is more efficient for that particular compiler and usage,
however it will tell you nothing about the general nature of the operations
(ie: any results you get will be highly compiler/platform/use dependant).
As most of us know (but just in case the OP doesn't) , these are not
the same operator:
++i evaluates to (i+1) with the side effect of incrementing i (in other
words - returns the value of I AFTER the increment)
i++ evaluates to (i) with the side effect of incrementing i (in other
words - return the value of I BEFORE the increment)
One may be more "efficient" than the other in a particular algorithm,
(for example using ++i in a case where you need the preincremented
value means either using (i-1) later, or another variable) - however
even in this case, most compilers will optimize the results to the same
or equivalent code - And who knows - some perverse architecture
could exist where the opposite of "what you expect" in terms of
efficiency is true. The point I am making is that you really cannot
make a correlation between individual C operations (or even groups
of operations) and efficiency. Write clean readable straightforward
code and trust the compiler to do it's job.
One might image that it *could* be easier for
the compiler to generate fewer instructions for ++i than i++ but I'd be
surprised if modern compilers couldn't detect either idiom and do the
smart thing in both cases.
This caught my eye (and is the main reason I responded) ... In very
early versions of my compiler, for certain register limited architectures,
i++ could generate
load i
increment
store i
decrement
And the compiler wasn't bright enough to recognize those operations
which were being performed for the side effect only, resulting in
extra an unneeded "decrement" operations when i++ was used
independantly, but not when ++i was used.
This artifact is long gone, and both operations will produce exactly the
same code in all architectures I support now - but it does provide a
real example of how one CAN be more efficient that the other IN A
GIVEN IMPLEMENTATION. The problem is there are no rules that
you can use to determine this.
When you are using these operations for the side effect only, it really
comes down to a matter of personal taste/style. Partly because of the
behaviour of my early compilers described above, but mostly because
I think "increment i" and not "i gets incremented", I code ++i instead of
i++ ... but I would not expect either to consistantly produce more efficient
(or even different) code than the other.
Regards,