M
Matt Garman
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 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
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 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