Jump to content
  • Advertisement
Sign in to follow this  
w166um

i++ or ++i

This topic is 5113 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

Well I know the difference between the two. However I read somewhere that ++i is more efficient, due to the fact that i++ uses a temp variable in its instructions. True? False?

Share this post


Link to post
Share on other sites
Advertisement
When using integers, it's a non-issue. When using iterators, which can of course be arbitrarily large, returning temporaries could be a bad thing. Since I don't think it sacrifices any readability, I always prefer preincrementing unless I have a specific reason to postincrement. (If it did sacrifice clarity, readability, or simplicity, it would be a foolish and vastly premature optimisation habit.)

Share this post


Link to post
Share on other sites
If you look at the assembler produced by your compiler you'll see how it is really done. On my experience(gcc), the compiler always produces excellent code, so discussing stuf like this(i++ v/s ++i) is a waste of time.

--
"Preoptimization is the root of all evil!"

Share this post


Link to post
Share on other sites
Quote:
Original post by borisnico
If you look at the assembler produced by your compiler you'll see how it is really done. On my experience(gcc), the compiler always produces excellent code, so discussing stuf like this(i++ v/s ++i) is a waste of time.

--
"Preoptimization is the root of all evil!"


Im kinda starting to be annoyed by the misquotes. Preoptimization isn't even a word.

Quote:
What Knuth actually said
We should forget about small efficiencies, about 97% of the time. Premature optimization is the root of all evil.


He was saying you should first consider a better algorithm, not giving you license to be lazy or to ignore the implications of your decisions.


Foo& Foo::operator++ ()
{
return *this;
}

Foo Foo::operator++ (int)
{
Foo temp = *this;
++(*this);
return temp;
}


You're going to rely on the compiler to recognize which you should be using?

[Edited by - wild_pointer on September 18, 2004 1:30:27 PM]

Share this post


Link to post
Share on other sites
I always use i++ when I'm only doing it to increase an integer, mostly out of habit, but also because I think it looks better ;) And it will when compiled and optimized be exactly the same as ++i.

In all other cases, I only use i++ when that's the functionality I want.

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!