Speed of iterating through STL containers

This is a discussion on Speed of iterating through STL containers within the C++ Programming forums, part of the General Programming Boards category; I can promise you I don't use debug builds for testing things....

  1. #16
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,752
    I can promise you I don't use debug builds for testing things.

  2. #17
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,532
    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;
    }
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    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.

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,159
    Curiously enough, my test also shows subscripting to be faster.
    hmm... but all your code does is compare incrementing iterators to incrementing unsigned longs.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #19
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,532
    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...
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    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.

  5. #20
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,532
    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.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    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.

  6. #21
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,159
    There does not seem to be a behavior justifying the "shouldn't matter" thesis, in any case.
    Well, if the number of element accesses is relatively small, improving other factors may make much more of a difference.

    It seems subscripting is only faster if one only traverses the container and does not read from or write to it.
    If there's no accessing of the elements, the subscript has not been used.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Noob STL question about Containers.
    By Swerve in forum C++ Programming
    Replies: 2
    Last Post: 03-15-2009, 01:02 PM
  2. Bit arrays with STL containers
    By Mario F. in forum C++ Programming
    Replies: 23
    Last Post: 07-03-2007, 10:50 AM
  3. stl containers allocation in heap or stack?
    By sawer in forum C++ Programming
    Replies: 9
    Last Post: 08-06-2006, 04:08 PM
  4. Pointer Elements & STL Containers :: STL
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 09-30-2002, 09:13 PM
  5. STL: element-wise addition of the contents of two containers
    By geophyzzer in forum C++ Programming
    Replies: 4
    Last Post: 06-26-2002, 07:17 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21