Possible Duplicate:
Is there a performance difference between i++ and ++i in C++?
I heard about that preincrements (++i) are a bit faster than postincrements (i++) in C++. Is that true? And what is the reason for this?
Possible Duplicate:
Is there a performance difference between i++ and ++i in C++?
I heard about that preincrements (++i) are a bit faster than postincrements (i++) in C++. Is that true? And what is the reason for this?
Post-increment usually involves keeping a copy of the previous value around and adds a little extra code. Pre-increment simply does it's job and gets out of the way. I typically pre-increment unless the semantics would change and post-increment is actually necessary.
Postincrements have to make a copy of the object to return the unincremented value. For class types, this could be significant but for "int-like" types (incl. pointers), it's probably not.
actually - it depends. Postincrement needs a copy since it preserves the old value. If the type incremented is a complex type (e.g. an iterator) and not a simple type, the preincrement is faster than the postincrement. That is only true of course if you don't need the value before the increment.
Some compilers might even detect that you don't need a postincrement and optimize the copy away - but as always - it's a bad habit to rely on that.
The short answer is: i++ has to make a copy of the object and ++i does not.
The long answer involves some code examples.
int i = 1;
int j = i++; // j is equal to 1, because i is incremented after the value is returned,
// which means a copy was made of the old value
int k = ++i; // k is equal to 3, because i is incremented, then returned. No copy is needed
Or a more robust example of the operator overloads:
class integer
{
public:
integer(int t_value)
: m_value(t_value)
{
}
int get_value() { return m_value; }
integer &operator++() // pre increment operator
{
++m_value;
return *this;
}
integer operator++(int) // post increment operator
{
integer old = *this; // a copy is made, it's cheap, but it's still a copy
++m_value;
return old; // Return old copy
}
private:
int m_value;
}
You might ask, "why cannot the C++ compiler optimize away the copy of you don't use it?" Really:
int i=1;
++i; //why is this
i++; //faster than this if I'm not using the copied returned value?
It's possible that the compiler may optimize away the copy for you, but it's also possible that your post-increment operator overload has other side effects that the compiler cannot optimize for without changing the meaning of your code.
On a side note, it's rare that you literally want to increment and return the old value, so ++i is often more semantically correct than i++, besides being more efficient.
PS : Copied from here.
On any decent compiler, ++i and i++ are identical if the value is not used.
If the value is used, ++i would need a temporary to store the pre-increment value if identical semantics were wanted.
The difference for plain types types such as int is probably negligible. I would guess the compiler is able to optimize such cases (e.g. turn a postfix into a prefix increment where appropriate). However, for complex types (typically STL iterators the difference might be noticeable). The compiler is not able to switch to prefix in these cases because the operators actually might do totally different things. I recommend reading STL Iterators and Performance for some background info on the performance penalty on using post-increment for STL iterators(short story: it takes twice the time as the pre-increment).