473,224 Members | 1,956 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,224 software developers and data experts.

vector performance: iterator versus subscripting

Is there any difference, performance-wise, accessing elements of a
vector using iterators or the subscript operator?

In other words, say I have a vector of strings:

vector<string> strvec;

After some processing, the vector has a very large (>50,000) number of
elements. Is there any performance difference in the following methods
of vector access?

vector<string>::iterator ii;
for (ii=strvec.begin(); ii!=strvec.end(); ++ii) {
// do stuff with *ii
}

Alternative:

unsigned long i;
for (i=0; i<strvec.size(); i++) {
// do stuff with strvec[i]
}

I did make a quick and dirty test of this: I read in about 5 MB worth of
strings (the strings were random numbers generated in another program by
rand()) into a vector. I then output those strings to files, once using
iterators and once using vector subscripting. There was no difference
in runtime.

That test obviously isn't at all complete, but I was hoping some
knowledgeable folks had the answer(s) :)

Thanks,
Matt
email at: http://raw-sewage.net/index.php?file=email

Jul 22 '05 #1
4 4297
"Matt Garman" <ga****@raw-sewage.bogus> wrote in message
news:sl*******************@news-proxy.cso.uiuc.edu...
| Is there any difference, performance-wise, accessing elements of a
| vector using iterators or the subscript operator?
Not a predictable difference, and most likely a negligible one.
....
| After some processing, the vector has a very large (>50,000) number of
| elements. Is there any performance difference in the following methods
| of vector access?
|
| vector<string>::iterator ii;
| for (ii=strvec.begin(); ii!=strvec.end(); ++ii) {
| // do stuff with *ii
| }
....
| unsigned long i;
| for (i=0; i<strvec.size(); i++) {
| // do stuff with strvec[i]
| }
Other factors are likely to affect performance much more than
using indexing vs. iterators on std::vector.
For example, loading the value of strvec.end() or strvec.size() into
a local variable instead of calling the function at every iteration is
likely to make a more important (albeit small) performance difference.

Ideally, if all functions of std::vector are inlined, a compiler could
generate optimal code for both loop implementations.

An advantage of iterators is that it that they can be more flexible
if you later need to work with a different container type,
standard algorithms, or a subrange of the container.

So I would personally write the loop as:
typedef vector<string>::iterator It;
It end = strvec.end();
for( It scan = strvec.begin() ; scan != end ; ++scan )
{
string const& str = *scan;
... do stuff with str ...
}

Or, better, use a function object and the std::for_each() algorithm...

| I did make a quick and dirty test of this: I read in about 5 MB worth of
| strings (the strings were random numbers generated in another program by
| rand()) into a vector. I then output those strings to files, once using
| iterators and once using vector subscripting. There was no difference
| in runtime.
The file I/O and memory allocations would in any case have outweighed
the performance difference between the two loop implementations.
I hope this helped,
Ivan
--
http://ivan.vecerina.com
Jul 22 '05 #2
> For example, loading the value of strvec.end() or strvec.size() into
a local variable instead of calling the function at every iteration is
likely to make a more important (albeit small) performance difference.


It is possible that the size or the last element of the vector changes
during the loop. Wouldn't that be a problem if .end() resp. .size() are
saved to local variables before?

Jul 22 '05 #3

"Harald Grossauer" <ha**************@uibk.ac.at> skrev i en meddelelse
news:3f******@sia.uibk.ac.at...
For example, loading the value of strvec.end() or strvec.size() into
a local variable instead of calling the function at every iteration is
likely to make a more important (albeit small) performance difference.


It is possible that the size or the last element of the vector changes
during the loop. Wouldn't that be a problem if .end() resp. .size() are
saved to local variables before?


Yes. In this case, you can not store the end() before the loop starts.
But do not be that obsessed with performance. Write the code, and if it runs
to slowly profile it and eliminate bottlenecks.

Kind regards
Peter
Jul 22 '05 #4
"Peter Koch Larsen" <pk*@mailme.dk> wrote in message
news:3f***********************@dread11.news.tele.d k...
| "Harald Grossauer" <ha**************@uibk.ac.at> skrev i en meddelelse
| news:3f******@sia.uibk.ac.at...
| > > For example, loading the value of strvec.end() or strvec.size() into
| > > a local variable instead of calling the function at every iteration is
| > > likely to make a more important (albeit small) performance difference.
| >
| > It is possible that the size or the last element of the vector changes
| > during the loop. Wouldn't that be a problem if .end() resp. .size() are
| > saved to local variables before?
| >
|
| Yes. In this case, you can not store the end() before the loop starts.
| But do not be that obsessed with performance. Write the code, and if it
runs
| to slowly profile it and eliminate bottlenecks.

Ah, the fine line between "premature optimization"
and "avoiding unnecessary pessimization" ;-)

In this case, I would consider it a matter of style (I have a
macro in my editor to automatically generate a for-each like loop
with a stored end bound).

But of course, as I had mentioned in my first reply, using for_each()
is a first choice if a functor can be provided easily.

Regards,
Ivan
--
http://ivan.vecerina.com
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Jim West | last post by:
I'm curious why I might be getting such a large performance difference between using a map iterator and a vector iterator. This is a computational electromagnetics code where I have to separate...
11
by: Steve | last post by:
Hi, I'm using a std::vector to store a list of user defined objects. The vector may have well over 1000 elements, and I'm suffering a performance hit. If I use push_back I get a much worse...
29
by: Hagen | last post by:
Hello, in a recent thread "speed of vector vs array" I read about the problem of the slow acces by addressing vector elements by indexing, unfortunately I see no workaround in my case. My...
19
by: daniel | last post by:
1) is C++ smart enough to automatically use "bits" for bool or will a bool have the size of a charcter (byte). 2) The index of a vector is it an integer (4 byte) or a "long long" with 8 bytes or...
13
by: zaineb | last post by:
Hi, This is a follow-up of sort of this thread:...
23
by: Sanjay Kumar | last post by:
Folks, I am getting back into C++ after a long time and I have this simple question: How do pyou ass a STL container like say a vector or a map (to and from a function) ? function: ...
6
by: zl2k | last post by:
hi, there I am using a big, sparse binary array (size of 256^3). The size may be changed in run time. I first thought about using the bitset but found its size is unchangeable. If I use the...
2
by: ma740988 | last post by:
typedef std::vector < std::complex < double > > complex_vec_type; // option1 int main() { complex_vec_type cc ( 24000 ); complex_vec_type dd ( &cc, &cc ); } versus
6
by: Michael | last post by:
Hi, I am new to C++ and have run into the following problem. I have written some functions which return a vector e.g vector<doublemyfunction(double arg1,.......,.....) { }
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.