Re: why not use i++ instead of ++i in for()?

From: Bo Persson (bop_at_gmb.dk)
Date: 02/23/04


Date: Mon, 23 Feb 2004 17:17:04 +0100


"Mark S" <mark110.DONTSPAMME@mindspring.com> skrev i meddelandet
news:dJb_b.15047$W74.4622@newsread1.news.atl.earthlink.net...
> "r norman" <rsn_@_comcast.net> wrote in message
> news:6due309in9sbtd7oa6hhn1lkp9bp3pairv@4ax.com...
> > On Sat, 21 Feb 2004 21:49:21 +0800, "snnn" <amethyst_sun@sohu.com>
> > wrote:
> > >yes,here,i++ and ++i has the same effect;
> > >but,if the type of i is an iterator of vector(or other else),++i is
more
> > >fast than i++;
> > >why not write all ++i instead?
> > >
> >
> > "Usually" depends on who is writing the code. You also say that the
> > pre-increment is faster than the post-increment. What gives you that
> > idea? If neither the pre-incremented value nor the post-incremented
> > value is used, I believe that most compilers will simply produce the
> > same code.
>
> Scott Meyer's "More Effective C++", "Item 6: Distinguish between prefix
and
> postfix forms of increment and decrement operators" goes into great detail
> about pre and postfix operators and what the differences are.
>
> When it comes to basic types like "int", I'm not sure if there is enough
of
> a difference to worry about, but as the previous poster points out, if i
is
> an iterator, or some other more complicated class, the pre-increment is
more
> efficient.
>
> The reason for this is that with a pre-increment operator, the object is
> incremented, and then it returns a reference to itself. The post-increment
> operator would need to save a copy of it's old value, increment itself,
and
> return that old copy by value. That results in extra temporary(s) and copy
> constructors being called.
>
> So I typically always use ++i, out of habit.
>
> Another thing I do when working with strings or other classes that give me
a
> choice between + and +=, when I want to concatenate a handful of strings:
>
> s = a + b + c + d;
>
> is not as efficient as

This all depends on the implementation, and the lengths of the strings
involved. If some, or all, of the strings are short, there might not be many
allocations at all.

>
> s = a;
> s += b;
> s += c;
> s += d;
>
> Again, think about the temporaries that are being used and returned.

But here you may have to reallocate the internal buffer of s up to 4 times.

Bo Persson

>
> -mark
>
>
>



Relevant Pages

  • Re: why not use i++ instead of ++i in for()?
    ... postfix forms of increment and decrement operators" goes into great detail ... about pre and postfix operators and what the differences are. ... The reason for this is that with a pre-increment operator, ... Another thing I do when working with strings or other classes that give me a ...
    (microsoft.public.vc.stl)
  • Re: why not use i++ instead of ++i in for()?
    ... postfix forms of increment and decrement operators" goes into great detail ... about pre and postfix operators and what the differences are. ... The reason for this is that with a pre-increment operator, ... Another thing I do when working with strings or other classes that give me a ...
    (microsoft.public.vc.stl)
  • Re: why not use i++ instead of ++i in for()?
    ... >postfix forms of increment and decrement operators" goes into great detail ... >about pre and postfix operators and what the differences are. ... possible for the compiler to select the most efficient implementation ...
    (microsoft.public.vc.stl)
  • Re: use of ++i, i++
    ... > From reading some guides i understood that prefix, postfix operate ... > this way, former executes the increment right away, latter executes the ...
    (comp.lang.c)
  • Re: Question on strncmp / strnicmp use
    ... to test the characters from both strings and increment two pointers. ... Using just mycomparison instead of strlen + mycomparison is one less O ... You mention increment of pointers and comparison, ...
    (comp.lang.c)