i++ or ++i in a for loop

i++ or ++i in a for loop

Post by Zyga » Thu, 20 Jul 2000 04:00:00



Is it better to write:
    for (int i=0; i<sth; i++)
or
    for (int i=0; i<sth; ++i)
I'm asking because some people seem to use the first version while other
people prefer the latter.
--
Zygmunt Krynicki  =/\=
 
 
 

i++ or ++i in a for loop

Post by Michael D. Obe » Thu, 20 Jul 2000 04:00:00


They mean the same.  I feel that i++ is easier to read because your object
(i) appears first.
--
Mike Ober


Quote:> Is it better to write:
>     for (int i=0; i<sth; i++)
> or
>     for (int i=0; i<sth; ++i)
> I'm asking because some people seem to use the first version while other
> people prefer the latter.
> --
> Zygmunt Krynicki  =/\=


 
 
 

i++ or ++i in a for loop

Post by Harr » Thu, 20 Jul 2000 04:00:00


Quote:> > Is it better to write:
> >     for (int i=0; i<sth; i++)
> > or
> >     for (int i=0; i<sth; ++i)
> > I'm asking because some people seem to use the first version while other
> > people prefer the latter.

> They mean the same.  I feel that i++ is easier to read because your object
> (i) appears first.

No they do _not_ mean the same. ++i  will add one to i and return the
result. i++ will make a copy of i, add one to i and return the _copy_ of i
from before it was incremented. In _this particular context_ it will make no
difference to what happens, since nobody is looking at the return value,
only the incremented i. In other situations it makes an enormous amount of
difference.

i++ is also slightly slower, since it has the overhead of making this copy.
Therefore, in this case, use ++i if you are incredibly tight for time and
this sort of nitty gritty will make a difference (unusual). Otherwise, use
whichever you think is easier to read.

 
 
 

i++ or ++i in a for loop

Post by Mark Wilde » Thu, 20 Jul 2000 04:00:00




Quote:> They mean the same.  I feel that i++ is easier to read because your object
> (i) appears first.

K&R used i++, perhaps for that reason. Current style is ++i, as in theory
it's a bit faster (no need to save the current value of i before the
increment). However, any good compiler would emit identical code for ++i and
i++ in a for clause.
 
 
 

i++ or ++i in a for loop

Post by Ron Natali » Thu, 20 Jul 2000 04:00:00



> i++ is also slightly slower, since it has the overhead of making this copy.
> Therefore, in this case, use ++i if you are incredibly tight for time and
> this sort of nitty gritty will make a difference (unusual). Otherwise, use
> whichever you think is easier to read.

Nope, in this case any compiler I've ever used generates the exact same
code (just increment the variable) as it knows the expression value is
not used.

However, you are right, ++i is a good habit.  If i were a class (like
an iterator), it is MUCH more efficient to do it this way.

 
 
 

i++ or ++i in a for loop

Post by richardl.. » Thu, 20 Jul 2000 04:00:00


If i is an 'int' then it doesn't really matter.
But if 'i' were a large object (a complex iterator, for instance), then
consider that ++i returns a reference but i++ returns an object, in
which case using ++i would be better.



Quote:> Is it better to write:
>     for (int i=0; i<sth; i++)
> or
>     for (int i=0; i<sth; ++i)
> I'm asking because some people seem to use the first version while
other
> people prefer the latter.
> --
> Zygmunt Krynicki  =/\=

Sent via Deja.com http://www.deja.com/
Before you buy.
 
 
 

i++ or ++i in a for loop

Post by Andrew Koen » Thu, 20 Jul 2000 04:00:00



>Is it better to write:
>    for (int i=0; i<sth; i++)
>or
>    for (int i=0; i<sth; ++i)
>I'm asking because some people seem to use the first version while other
>people prefer the latter.

The second form is better, because the first form explicitly
requests a computation that it then throws away.

That is, writing

        i++;

is equivalent to writing

        j = i;
        ++i;

Why did you ask for the assignment to j if you didn't want to
use the value of j afterward?

Now, of course if i is an integer, this whole discussion is
ridiculously pedantic:  Every compiler I've seen will optimize
away the extra computation.  However, this optimization becomes
much more difficult if i, instead of being an integer or a pointer,
is an iterator of user-defined type.

In other words, writing i++ instead of ++i may have significant
run-time overhead on real implementations if i is an iterator
type.  Why not get into the right habit early by writing ++i all
the time unless you're going to use the result?
--

 
 
 

i++ or ++i in a for loop

Post by Dietmar Kueh » Thu, 20 Jul 2000 04:00:00


HI,


Quote:> Is it better to write:
>     for (int i=0; i<sth; i++)
> or
>     for (int i=0; i<sth; ++i)

If 'i' is of type 'int' or some other built-in type, it does not really
matter: The generated code is almost certainly identical. However, in
C++ especially after the introduction of the algorithms and containers
library you will have loops often look similar to those given above (if
you have loops at all, that is) except that 'i' is of some interator
type. In this case, the code suddenly stops to be identical and instead
using 'i++' is more involved than '++i'. Depending on the iterator type
the post increment can be *much* slower (although in general this is
not that harmful). Thus, it I consider it good practise to use '++i'
wherever possible. Only where really required I'm using 'i++', eg. when
removing elements from a list:

  while (i != l.end())
    l.erase(++i);

except, of course, that this code would rather be written as
'l.erase(i, l.end())'
--

<http://www.dietmar-kuehl.de/>

Sent via Deja.com http://www.deja.com/
Before you buy.

 
 
 

i++ or ++i in a for loop

Post by Michael D. Obe » Thu, 20 Jul 2000 04:00:00


The key to this question is the context.  i++ and ++i are identical in this
context.

The other comments about using ++i for performance reasons when i is an
iterator class are excellent.
--
Mike Ober


Quote:> > > Is it better to write:
> > >     for (int i=0; i<sth; i++)
> > > or
> > >     for (int i=0; i<sth; ++i)
> > > I'm asking because some people seem to use the first version while
other
> > > people prefer the latter.

> > They mean the same.  I feel that i++ is easier to read because your
object
> > (i) appears first.

> No they do _not_ mean the same. ++i  will add one to i and return the
> result. i++ will make a copy of i, add one to i and return the _copy_ of i
> from before it was incremented. In _this particular context_ it will make
no
> difference to what happens, since nobody is looking at the return value,
> only the incremented i. In other situations it makes an enormous amount of
> difference.

> i++ is also slightly slower, since it has the overhead of making this
copy.
> Therefore, in this case, use ++i if you are incredibly tight for time and
> this sort of nitty gritty will make a difference (unusual). Otherwise, use
> whichever you think is easier to read.

 
 
 

i++ or ++i in a for loop

Post by E. Robert Tisdal » Thu, 20 Jul 2000 04:00:00



> Now, of course if i is an integer,
> this whole discussion is ridiculously pedantic:

Silly?

Quote:> Every compiler I've seen will optimize away the extra computation.
> However, this optimization becomes much more difficult
> if i, instead of being an integer or a pointer,
> is an iterator of user-defined type.

> In other words, writing i++ instead of ++i may have significant
> run-time overhead on real implementations if i is an iterator type.

Wouldn't that depend upon the implementation?

Quote:> Why not get into the right habit early
> by writing ++i all the time
> unless you're going to use the result?

I don't think it matters.
 
 
 

i++ or ++i in a for loop

Post by Ron Natali » Thu, 20 Jul 2000 04:00:00



> > In other words, writing i++ instead of ++i may have significant
> > run-time overhead on real implementations if i is an iterator type.

> Wouldn't that depend upon the implementation?

Probably not.  The implementation is NOT free to assume that an
overloaded preincrement operator is the same as the overloaded
postincrement operator and substitute one for the other.

Quote:

> > Why not get into the right habit early
> > by writing ++i all the time
> > unless you're going to use the result?

> I don't think it matters.

?
 
 
 

i++ or ++i in a for loop

Post by Andrew Koen » Thu, 20 Jul 2000 04:00:00




Quote:>> In other words, writing i++ instead of ++i may have significant
>> run-time overhead on real implementations if i is an iterator type.
>Wouldn't that depend upon the implementation?

Yes it would.  However, it is very hard to imagine an
implementation in which ++i would be slower than i++,
and very easy to imagine one on which ++i would be
much faster.  Remember, we're talking here about i having
a class type, in which a typical implementation looks like this:

        class Iterator {
        public:
                // ...

                Iterator& operator++() {    // prefix
                        // do something here
                        return *this;
                }

                Iterator operator++(int) {      // postfix
                        Iterator ret = *this;
                        ++*this;
                        return ret;
                }

                // ...
        };

In such an implementation, using postfix ++ will never be faster than
using prefix ++, and will often be slower.

So prefix ++ has two advantages:

        1. It might be faster.

        2. It doesn't request an extra computation and then throw
           the result away.

I am not aware of any advantages of using postfix ++ aside from habit.

Quote:>> Why not get into the right habit early
>> by writing ++i all the time
>> unless you're going to use the result?
>I don't think it matters.

It doesn't matter much most of the time.  Sometimes it does, though.
--

 
 
 

i++ or ++i in a for loop

Post by Richard VanHoute » Sun, 23 Jul 2000 04:00:00



> Is it better to write:
>     for (int i=0; i<sth; i++)
> or
>     for (int i=0; i<sth; ++i)
> I'm asking because some people seem to use the first version while other
> people prefer the latter.

I prefer "++i" in for loops because (unless you have a really smart
optimizer) it is slightly more efficient.

What occurs during i++:

Load register
Increment register
Store register
Decrement register (result of decrement is discarded)

What occurs during ++i:
Load register
Increment register
Store register

How significant is that extra decrement?  That depends on how often
the loop is executed, and how much is being done inside the loop.

 
 
 

i++ or ++i in a for loop

Post by John Celme » Sun, 23 Jul 2000 04:00:00


If i is a user-defined type, Scott Meyers in his book More Effective C++,
item 6, has a very good explanation.  Basically, the prefix version is more
efficient.



> > Is it better to write:
> >     for (int i=0; i<sth; i++)
> > or
> >     for (int i=0; i<sth; ++i)
> > I'm asking because some people seem to use the first version while other
> > people prefer the latter.

> I prefer "++i" in for loops because (unless you have a really smart
> optimizer) it is slightly more efficient.

> What occurs during i++:

> Load register
> Increment register
> Store register
> Decrement register (result of decrement is discarded)

> What occurs during ++i:
> Load register
> Increment register
> Store register

> How significant is that extra decrement?  That depends on how often
> the loop is executed, and how much is being done inside the loop.