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
    Chi! whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    8,189
    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
    Angola
    Posts
    7,909
    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.

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,623
    Curiously enough, my test also shows subscripting to be faster.
    hmm... but all your code does is compare incrementing iterators to incrementing unsigned longs.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    Angola
    Posts
    7,909
    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.

  5. #20
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Angola
    Posts
    7,909
    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.

  6. #21
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,623
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 12:02 PM
  2. Bit arrays with STL containers
    By Mario F. in forum C++ Programming
    Replies: 23
    Last Post: 07-03-2007, 09:50 AM
  3. stl containers allocation in heap or stack?
    By sawer in forum C++ Programming
    Replies: 9
    Last Post: 08-06-2006, 03:08 PM
  4. Pointer Elements & STL Containers :: STL
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 09-30-2002, 08: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, 06:17 PM

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