I can promise you I don't use debug builds for testing things.
Printable View
I can promise you I don't use debug builds for testing things.
Curiously enough, my test also shows subscripting to be faster.
Code:#include <cstdlib>
#include <iostream>
#include <vector>
#include <iterator>
#include <ctime>
using namespace std;
int main()
{
vector<int> vec(10000000, 1);
vector<int> vec2(10000000, 3);
vector<int>::iterator iter = vec.begin();
vector<int>::iterator iter2 = vec2.begin();
clock_t start_iter, end_iter;
start_iter = clock();
cout << "Iterators ----\nStart: " << start_iter << flush;
for(; iter != vec.end(); ++iter) {
for (; iter2 != vec2.end(); ++iter2) {
;
}
}
end_iter = clock();
cout << "\nEnd: " << end_iter << "\nClock: " << end_iter - start_iter << endl;
start_iter = clock();
cout << "\nSubscripting ----\nStart: " << start_iter << flush;
for(unsigned long i = 0; i != vec.size(); ++i) {
for(unsigned long j = 0; j != vec2.size(); ++j) {
;
}
}
end_iter = clock();
cout << "\nEnd: " << end_iter << "\nClock: " << end_iter - start_iter << endl;
system("pause");
return EXIT_SUCCESS;
}
hmm... but all your code does is compare incrementing iterators to incrementing unsigned longs.Quote:
Curiously enough, my test also shows subscripting to be faster.
Very true.
However, changing the null statements to int some_int = *iter + *iter2 and [I]int some_int = vec + vec2[j] respectively, shows subscripting to be much, much slower than iterators this time...
There does not seem to be a behavior justifying the "shouldn't matter" thesis, in any case.
When I changed to *iter = *iter2 and [I]vec = vec2[j] to test assignment, the results seemed consistent with subscripting being much, much, slower.
It seems subscripting is only faster if one only traverses the container and does not read from or write to it.
Well, if the number of element accesses is relatively small, improving other factors may make much more of a difference.Quote:
There does not seem to be a behavior justifying the "shouldn't matter" thesis, in any case.
If there's no accessing of the elements, the subscript has not been used.Quote:
It seems subscripting is only faster if one only traverses the container and does not read from or write to it.