i++ vs ++i

Started by
39 comments, last by Raghar 18 years, 8 months ago
whats the difference between i++ and ++i? i see it used alot in loops. what is the advantage of either method?
Advertisement
++i is pre-incrementation, and i++ is post-incrementation. They basically mean, increment before the value is evaluated, or increment after the value is evaluated, respectively.

If you did:

int i = 1;
std::cout << ++i; // <-- This outputs 2
std::cout << i; // <-- This outputs 2

But if you did:

int i = 1;
std::cout << i++; // <-- This outputs 1
std::cout << i; // <-- This outputs 2

Generally, its more efficient to do ++i, because i++ requires storing an intermediate value, incrementing, and then returning. For good practice, you should do ++i whenever you can, unless your real intended meaning is i++. However, modern compilers will do optimizations that will filter-out useless i++ statements and turn them into the equivalent of ++i.

In the case of loops, more specifically for loops, it makes no difference, because the incrementation is always done after each cycle of the loop. So whether you post-increment or pre-increment. Its done at the same time and the variable takes on the same value.

Looking for a serious game project?
www.xgameproject.com
i++ increments the value and returns the old value. ++i increments the value and returns the new value. For integers there is generally no performance difference, for more complex classes that implement operator++(), such as many iterators, generally you should use the ++i form unless you need the old value.
Quote:Original post by Max_Payne
++i is pre-incrementation, while i++ is post-incrementation. They basically mean, increment before the value is evaluated, or increment after the value is evaluated, respectively.


To put it in more simple terms:

i = 3;x = i++;  //x is equal to 3, but i is now 4.j = 3;y = ++i;  //both y and j are equal to 4.
If you can go either way, my vote goes to i++ because the gain in readability far outweights the infinitesimal gain in performance.

But SiCrane's note about ++i for iterators is good.
that was quick! thanks alot for clearing that up for me.

damn i love this forum
The part no one has mentioned is this ... the most common use of i++ is in situations like this:

for(int i = 0; i < length; i++)

this is just plain silly. when executed as a full stament (not inside a bigger expression or statement), the results of pre and post increment are identical ... the value gets incremented by the end of the stament ... but the theory of the i++ version says that an intermediate is created to return the old value. While it is totally true that this gets optimized away for all integral types on all modern compilers ... it DOES NOT get optimized away if i is a custom class ...

for if you have a template written like this

template <typename IteratorType>
void DoSomething(IteratorType begin, IteratorType end)
{
while(begin != end)
{
// Do Something Here

begin++; // this is just bad, it should be ++begin
}
}

// all three of these are for incrementing i
i = i + 1;
i += 1;
++i;

// this is for post incrementing i, ONLY used for cases where you need to cacluate based on the OLD value of i
i++
++i; is supposed to give you a small speed improvement.
*News tagenigma.com is my new domain.
If you don't use the intermediate value of 'var++', the compiler removes all the unnecessary code... even in the case of complex stuff like iterators.

This assumes you have a 'good' optimizer.

Yes, compilers ARE this smart. Assembly-level dependency graphing is freaking awesome.

Just remember, when you're looking at assembly, if you're using a debug build, you're probably not seeing optimized results.
Quote:Original post by tgraupmann
++i; is supposed to give you a small speed improvement.


When i is an iterator, i++ may be slower than ++i. Key words: iterator, may. It should provide no noticable difference when i is an integer, nor is it guaranteed to be faster even when i is an iterator (although it should not be slower).

As such, it is good to get in the habit of writing the increment of loops as ++i rather than i++:

for ( ... ; ... ; ++i )
rather than:
for ( ... ; ... ; i++ )
(the two statements are equivilant otherwise)

However, do not avoid i++ on the basis that "it will be slower" when it will make your code clearer.

This topic is closed to new replies.

Advertisement