츌처: http://stackoverflow.com/questions/24901/is-there-a-performance-difference-between-i-and-i-in-c


[Executive Summary: Use ++i if you don't have a specific reason to use i++.]

For C++, the answer is a bit more complicated.

If i is a simple type (not an instance of a C++ class), then the answer given for C ("No there is no performance difference") holds, since the compiler is generating the code.

However, if i is an instance of a C++ class, then i++ and ++i are making calls to one of the operator++ functions. Here's a standard pair of these functions:

Foo& Foo::operator++() // called for ++i { this->data += 1; return *this; } Foo Foo::operator++(int ignored_dummy_value) // called for i++ { Foo tmp(*this); // variable "tmp" cannot be optimized away by the compiler ++(*this); return tmp; }

Since the compiler isn't generating code, but just calling an operator++ function, there is no way to optimize away the tmp variable and its associated copy constructor. If the copy constructor is expensive, then this can have a significant performance impact.

(Thanks to Paul for inquiring about the difference between C and C++.)



for루프에서

만일 i가 스칼라타입이면 걍 ++i든 i++든 큰 문제없다.

그러나 i가 객체라면 ++i를 써라.

특히 i가 iterator나 template 타입이면 반드시 ++i를 쓰자