On Nov 1, 10:41 pm, Erik Wikström <Erik-wikst...@telia.comwrote:
Quote:
On 2007-11-01 17:43, mosw...@gmail.com wrote:
[...]
Quote:
If we are talking about the same thing then the point he was
making was that due to cache latency (and RAM for large
collections) and the speculative prefetching modern CPUs
performs a traversal of a vector from start to end (or in
reverse) will be much faster (different magnitude) then a
traversal of a list or other node-based structure.
A vector generally does have better locality, but some list
implementations use various strategies to improve the locality
as well. Note too that if the objects in your vector use
dynamic memory (e.g. a vector of std::string), then you may not
have the locality anyway, even with vector.
Quote:
So if you will perform many traversals of the container but
very few insertions in the middle then a vector will be much
faster, however if you do only a few traversals but many
insertions in the middle then I am not so sure. As always (and
as Sutter said) always measure.
Always measure. If your copy constructor and assignment
operator are very expensive (e.g. a deep copy of dynamically
allocated memory), insertion in the middle of a vector becomes
very expensive. If they're cheap (e.g. for int), you have to
copy an awful lot for the cost to be as high as the dynamic
allocation a list typically needs (but here, too, some
implementations have more or less effective optimization
strategies).
Anytime you read global statements as to which container is
faster, be suspicious, because the only correct global answer
is: "it all depends".
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34