Possible Duplicate:
Is there a performance difference between i++ and ++i in C++?
Hi, I was asked someday ago that which one is faster variable++ or ++variable? I was little confuse. Can someone tell me which one is faster and why??
I think pre-increment would be faster, since it just increments it then and there and the deed is done, whilst post-incrementing requires keeping a copy of said variable a little longer.
This probably depends on the compiler, but I generally use pre-increment unless needed otherwise.
In C++ the answer I've personally been given is something along the lines of:
Post increment must create a copy of the objects current state, increment the object, and return the copy by value. For integer types this is irrelevant as by-value returns are the same size and the copy means little.
Pre-increments may simply increment and return itself by reference.
There are few answers out there:
http://www.geekinterview.com/question_details/14424
http://www.digitalpeer.com/id/where
but this question is a duplicate
Preincrement faster than postincrement in C++ - true? If yes, why is it?
---andrew
精彩评论