Possible Duplicate:
Is there a performance difference between i++ and ++i in C++?
I had an interview today and the interviewer asked me this question.
In C++ which is more “EFFICIENT” on non primitive types, A pre increment (++i) or a post increment (i++). I answered it by saying Pre increment but was not able to give the exact reason.
I have searched through the internet and found that Pre increment is more efficient. But I could not understand the reason. Can anyone please explain me the reason?
ex:
for ( ; c.value() != 21 ; ++c)
or
for ( ; c.value() != 21 ; c++)
pre-increment (
++i) is usually faster since post-increment returns the current value and then increments the value, whereas pre-increment just increments the variable.In many situations a compiler will optimise this anyway, unless you are using post-increment specifically to use the current value and increment the value afterwards.