Jump to content
  • Advertisement
Sign in to follow this  
adam17

i++ vs ++i

This topic is 4843 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

whats the difference between i++ and ++i? i see it used alot in loops. what is the advantage of either method?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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++

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!