[Effective-cpp] Item 1: Uses and Abuses of vector

Gregory Haley ghaley at venaca.com
Wed Oct 27 11:56:04 EDT 2004


>  I find it a bit frustrating that noone has addressed my 
> question so far.  Since AFAIK end() is not necessarily 
> calculated, as well as even if it is it is inline, I only see 
> a disaster waiting to happen due to premature optimisation, 
> and no benefit whatsoever.
> 
> Can anyone show any measurements or any fact/proof that this 
> optimization is actually worth the risk of broken code?  So 
> far I could only see opinions but not the facts they are 
> based on.  I am not implying they aren't based on facts, all 
> I say is that those facts have either not been mentioned or I 
> have managed to miss them.  I have seen it being mentioned 
> that end() is calculated, while the implementation I have 
> seen so far do not calculate
> anything:

Maybe you missed this from my reply to a different part of the thread:

<quote>
I must confess, I took Herb at his word on this when I brought it up for
summary, but the discussion caused me to examine the question more.

It made me curious about what the performance might be, so I wrote a couple
of little programs to build up a really long vector of a million ints, then
loop through it and print out the values (dumping the output to /dev/null)
and timed it.  There was no appreciable difference between the two variants
-- indeed what I had thought would be the more efficient loop was slightly
slower.  The platform was linux, the compiler g++ 3.2.3.  So my guess would
be that this compiler does not recalculate the value of the end iterator
each time.

-- snip --

On another point raised, I would "not" use this sort of loop for a vector
that I intend to modify, especially if the modification involved removing
items.  I learned this the difficult way, once trying to iterate through a
vector to remove elements, and at times, the removal would blow the "end
iterator" away.  
</quote>

(in my original post i accidentally left out the "not")

> So could somebody please address my questions?  I am really 
> puzzled now, because my experience shows that it makes no 
> sense to pre-evaluate end() (since it only adds the 
> possibility of error but no speed improvements) however all 
> the other people discussing here seems to be absolutely 
> convinced about the opposite.  One of us should have slightly 
> distorted view of this topic, and I am more than willing to 
> admit that I am the one - but please give some facts you base 
> your opinion on.

So your issue has been addressed in a number of places.

ciao!
greg.

Gregory Haley

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.779 / Virus Database: 526 - Release Date: 10/19/2004
 




More information about the Effective-cpp mailing list