Re: Any difference in iteration speed

Discussion in 'C++' started by Matthew Burgess, Aug 19, 2003.

  1. On Wed, 20 Aug 2003 09:44:03 +1200
    "Andrew Ward" <> wrote:

    > Between this:
    >
    > vector<int> vi;
    >
    > for(vector<int>::iterator it = vi.begin();it != vi.end();it++) {}
    >
    > and this:
    >
    > vector<int>::iterator end = vi.end();
    > for(vector<int>::iterator it = vi.begin();it != end;it++) {}
    >
    > In other words, does the reapplication of end() slow things down at
    > all? Also, is there any difference between it++ and ++it in this
    > situation?


    I'm just a noob but here's my opinion for what it's worth.

    A decent optimizing compiler may be able optimize away the repeated
    vi.end() calls in the first loop. As far as the difference between it++
    and ++it:

    I believe it is always more efficient to call the prefix version (++it)
    than the postfix, simply because the postfix has to construct a
    temporary object in order to return the original value of "it" before
    the increment.

    e.g. ++it will merely increment it whereas it++ will have to do
    something akin to:

    original_it = it;
    it+=1;
    return original_it.

    I'd be interested to hear if my understanding of these issues are
    correct :)

    Cheers,

    Matt.
     
    Matthew Burgess, Aug 19, 2003
    #1
    1. Advertising

  2. Matthew Burgess

    Gavin Deane Guest

    Matthew Burgess <> wrote in message news:<>...
    > On Wed, 20 Aug 2003 09:44:03 +1200
    > "Andrew Ward" <> wrote:
    >
    > > Between this:
    > >
    > > vector<int> vi;
    > >
    > > for(vector<int>::iterator it = vi.begin();it != vi.end();it++) {}
    > >
    > > and this:
    > >
    > > vector<int>::iterator end = vi.end();
    > > for(vector<int>::iterator it = vi.begin();it != end;it++) {}
    > >
    > > In other words, does the reapplication of end() slow things down at
    > > all? Also, is there any difference between it++ and ++it in this
    > > situation?

    >
    > I'm just a noob but here's my opinion for what it's worth.
    >
    > A decent optimizing compiler may be able optimize away the repeated
    > vi.end() calls in the first loop. As far as the difference between it++
    > and ++it:
    >
    > I believe it is always more efficient to call the prefix version (++it)
    > than the postfix, simply because the postfix has to construct a
    > temporary object in order to return the original value of "it" before
    > the increment.
    >
    > e.g. ++it will merely increment it whereas it++ will have to do
    > something akin to:
    >
    > original_it = it;
    > it+=1;
    > return original_it.


    Assuming we are still in the context of the for loop, if the iterator
    is a simple type like int*, your decent optimising compiler will
    probably optimise away original_it because you aren't using the value.
    So there will be no difference between ++it and it++.

    That said, I don't see any benefit in trying to guess the compiler's
    behaviour every time I want to increment something so I always use
    pre-increment (in for loops and elsewhere) unless I need to use the
    original value in the same expression.

    GJD
     
    Gavin Deane, Aug 20, 2003
    #2
    1. Advertising

  3. Matthew Burgess

    Rolf Magnus Guest

    Matthew Burgess wrote:

    > On Wed, 20 Aug 2003 09:44:03 +1200
    > "Andrew Ward" <> wrote:
    >
    >> Between this:
    >>
    >> vector<int> vi;
    >>
    >> for(vector<int>::iterator it = vi.begin();it != vi.end();it++) {}
    >>
    >> and this:
    >>
    >> vector<int>::iterator end = vi.end();
    >> for(vector<int>::iterator it = vi.begin();it != end;it++) {}
    >>
    >> In other words, does the reapplication of end() slow things down at
    >> all? Also, is there any difference between it++ and ++it in this
    >> situation?

    >
    > I'm just a noob but here's my opinion for what it's worth.
    >
    > A decent optimizing compiler may be able optimize away the repeated
    > vi.end() calls in the first loop.


    Only with special knowledge about vi.end(). Without that knowledge, the
    compiler can't know that the return value of vi.end() is always the
    same, so it has to call it in every iteration. The compiler may have
    that knowledge since the iterator is part of the standard library, but
    I have no ideas if compilers do that kind of optimization.

    > As far as the difference between
    > it++ and ++it:
    >
    > I believe it is always more efficient to call the prefix version
    > (++it) than the postfix, simply because the postfix has to construct a
    > temporary object in order to return the original value of "it" before
    > the increment.


    If the operator++ is inlined, the compiler can find out that the copy is
    never used and optimize it away, but since there is no reason why
    prefix++ could be slower than postfix, it's a good idea to always use
    prefix++. Btw, if you need the old value of the iterator, it might
    still be faster to copy the old value yourself and then use prefix++ to
    increment the iterator.
     
    Rolf Magnus, Aug 20, 2003
    #3
  4. Matthew Burgess

    Axter Guest

    Originally posted by Matthew Burgess

    > On Wed, 20 Aug 2003 09:44:03 +1200


    > "Andrew Ward" <> wrote:


    >


    > > Between this:


    > >


    > > vector<int> vi;


    > >


    > > for(vector<int>::iterator it = vi.begin();it != vi.end();it++)

    > {}


    > >


    > > and this:


    > >


    > > vector<int>::iterator end = vi.end();


    > > for(vector<int>::iterator it = vi.begin();it != end;it++)

    > {}


    > >


    > > In other words, does the reapplication of end() slow things down

    > at


    > > all? Also, is there any difference between it++ and ++it in

    > this


    > > situation?


    >


    > I'm just a noob but here's my opinion for what it's worth.


    >


    > A decent optimizing compiler may be able optimize away the repeated


    > vi.end() calls in the first loop. As far as the difference
    > between it++


    > and ++it:


    >


    > I believe it is always more efficient to call the prefix
    > version (++it)


    > than the postfix, simply because the postfix has to construct a


    > temporary object in order to return the original value of "it" before


    > the increment.


    >


    > e.g. ++it will merely increment it whereas it++ will have to do


    > something akin to:


    >


    > original_it = it;


    > it+=1;


    > return original_it.


    >


    > I'd be interested to hear if my understanding of these issues are


    > correct :)


    >


    > Cheers,


    >


    > Matt.




    In general, prefix is faster then posfix, however most modern compilers
    will optimize away the difference for any built-in type.

    So with a built-in type, you will not see the difference in performance
    between prefix and posfix.


    --
    Top ten Expert at Experts-Exchange


    Posted via http://dbforums.com
     
    Axter, Aug 20, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ham

    I need speed Mr .Net....speed

    Ham, Oct 28, 2004, in forum: ASP .Net
    Replies:
    6
    Views:
    2,345
    Antony Baula
    Oct 29, 2004
  2. efiedler
    Replies:
    1
    Views:
    2,054
    Tim Ward
    Oct 9, 2003
  3. Russell Hanneken

    Re: Any difference in iteration speed

    Russell Hanneken, Aug 19, 2003, in forum: C++
    Replies:
    4
    Views:
    367
    Kevin Goodsell
    Aug 20, 2003
  4. Rudi
    Replies:
    5
    Views:
    5,031
  5. Sven C. Koehler
    Replies:
    0
    Views:
    124
    Sven C. Koehler
    Oct 2, 2009
Loading...

Share This Page