I can promise you I don't use debug builds for testing things.
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; }
Originally Posted by brewbuck:
Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.
hmm... but all your code does is compare incrementing iterators to incrementing unsigned longs.Curiously enough, my test also shows subscripting to be faster.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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...
Originally Posted by brewbuck:
Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.
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.
Originally Posted by brewbuck:
Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.
Well, if the number of element accesses is relatively small, improving other factors may make much more of a difference.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.It seems subscripting is only faster if one only traverses the container and does not read from or write to it.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)