i++ or ++i

Started by
6 comments, last by Jolle 19 years, 7 months ago
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?
Advertisement
From my dev journal: Post- or Pre-Increment.
True.
Not giving is not stealing.
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.)
Use ++i where its not necessary to use i++, for the good habit. In most cases you will find you almost never need to use i++.

Looking for a serious game project?
www.xgameproject.com
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!"
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]
[size=2]
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.

This topic is closed to new replies.

Advertisement