Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Re: Any difference in iteration speed

Reply
Thread Tools

Re: Any difference in iteration speed

 
 
Matthew Burgess
Guest
Posts: n/a
 
      08-19-2003
On Wed, 20 Aug 2003 09:44:03 +1200
"Andrew Ward" <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
 
Gavin Deane
Guest
Posts: n/a
 
      08-20-2003
Matthew Burgess <(E-Mail Removed)> wrote in message news:<(E-Mail Removed) land.ac.uk>...
> On Wed, 20 Aug 2003 09:44:03 +1200
> "Andrew Ward" <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      08-20-2003
Matthew Burgess wrote:

> On Wed, 20 Aug 2003 09:44:03 +1200
> "Andrew Ward" <(E-Mail Removed)> 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.

 
Reply With Quote
 
Axter
Guest
Posts: n/a
 
      08-20-2003

Originally posted by Matthew Burgess

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


> "Andrew Ward" <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[ANN] Localmemcache-0.4.3: Improving Iteration Speed & Bugfixes Sven C. Koehler Ruby 0 10-02-2009 09:43 PM
Struts - Problem with nested iteration or double iteration Rudi Java 5 10-01-2008 03:30 AM
501 PIX "deny any any" "allow any any" Any Anybody? Networking Student Cisco 4 11-16-2006 10:40 PM
speed speed speed a.metselaar Computer Support 14 12-30-2003 03:34 AM
Re: Any difference in iteration speed Russell Hanneken C++ 4 08-20-2003 12:42 AM



Advertisments