help-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Amazing Performance Difference between vec[] and iterators


From: Andre Poenitz
Subject: Re: Amazing Performance Difference between vec[] and iterators
Date: Wed, 26 Jul 2006 19:34:33 +0200

Mark <none_not@nadaspam.com> wrote:
>> Likewise, instead of
>> 
>> for(vector<int>::const_iterator it = v.begin(); it != v.end(); ++it)
>> 
>> try
>> 
>> for(vector<int>::const_iterator it = v.begin(), end = v.end(); it !=
>> end; ++it)
>> 
>> to avoid calling v.end() every time.
>> 
> 
> Aren't compilers smart enough yet to in effect do that if it sees there's
> nothing done inside the loop that would change the vector's size?
> That seems like something an optimizer should do.

It seems a very unlikely thing an optimizer _can_ do.

It would basically need to inspect all the code inside the loop and make
sure that the vector is not changed directly or by some external
reference - and this is pretty unlikely if there is e.g. a call to some
function in some different translation unit.

It is not much more effort to write

 for(vector<int>::const_iterator it = v.begin(), end = v.end(); it !=
 end; ++it)

instead of

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

and you can be sure that the end() call is evaluated only once.
 
Andre'



reply via email to

[Prev in Thread] Current Thread [Next in Thread]