473,397 Members | 1,969 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,397 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 4307
"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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.