Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

What is fastest?

0 views
Skip to first unread message

carl

unread,
Nov 30, 2009, 2:02:52 PM11/30/09
to
What is fastest, indexing or iterating a std::vector:


std::vector<std::string> container;
// Add 100000 strings to container

std::vector<std::string>::iterator it = container.begin()
while (it != container.end()) {
std::string str = *it;
// do something with str
it++;
}


or

int size = container.size();
for (int i=0; i< size; i++) {
std::string str = container[i];
// do something with str
}

On this page:

http://stackoverflow.com/questions/776624/whats-faster-iterating-an-stl-vector-with-vectoriterator-or-with-at


there are some different opinions, but what are your experinces?

Victor Bazarov

unread,
Nov 30, 2009, 2:19:54 PM11/30/09
to

In my experience, those things are compiler-specific, system-specific,
and cannot be theorised upon. They need to be measured.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

Bo Persson

unread,
Nov 30, 2009, 2:26:42 PM11/30/09
to

I think that "different opinions" is the correct result :-)

It varies some, but the difference is VERY small. The "do something
with str" is highly likely to dominate the timing anyway.


Bo Persson


0 new messages