Results 1 to 10 of 10

Thread: Iterator performance

  1. #1
    Join Date
    Jan 2011
    Posts
    34
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Iterator performance

    Many times I have read that using "for(int i=0; i<size; i++)" to iterate list is bad idea and we must use STL or Java style iterators. I decided to make my own research and calculate processing time for each iterating method. I made 5 trials and calculated average time.

    Qt Code:
    1. QList<int> list;
    2. int size = 100000000;
    3. for(int i = 0; i < size; i++)
    4. list.append(1);
    5. int sum = 0;
    6. qint64 begin = QDateTime::currentMSecsSinceEpoch();
    7.  
    8. //Simplest method
    9. for(int i = 0; i < size; i++)
    10. sum += list.at(i);
    11.  
    12. //Java-style iterator
    13. QListIterator<int> it(list);
    14. while(it.hasNext())
    15. sum += it.next();
    16.  
    17. //STL-style iterator
    18. QList<int>::iterator i;
    19. for(i = list.begin(); i != list.end(); ++i)
    20. sum += *i;
    21.  
    22. qint64 time = QDateTime::currentMSecsSinceEpoch() - begin;
    23. qDebug() << "Time: " << time;
    To copy to clipboard, switch view to plain text mode 
    Results surprised me. Simplest "stupid" method took 85 ms, Java-style iterator 111 ms and STL-iterator 260 ms. It seems that for simple iteration over all list's elements "for(int i=0; i<size; i++)" method will be the fastest.

  2. #2
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Iterator performance

    It's very hard to beat the performance of a simple pointer increment. This operation is normally available right on the CPU hardware and executes in a clock cycle (or less, in some circumstances).

    Iterators are nice because they provide a level of abstraction between the operation and the implementation; this brings several advantages, including the ability to swap in a new implementation without disturbing existing code, and the provision of a consistent interface for similar functionality across a broad range of objects. But like all things, abstraction comes at a cost, and often incurs a performance penalty. Which is more important - raw performance or maintainability - is up to the programmer.

    Honestly, though, the cost of loop overhead is normally very, very small in comparison with the time spent performing the actual workings within the loop. You will usually get much more benefit using the clearer, more portable abstractions and simply kicking the compiler's optimization setting up a notch or two than you'll gain by other means.

    Note, also, that your tests are not valid. In the STL case, you call list.end() on each iteration of the loop, incurring an unnecessary function call. You also mix direct dereference with calls to list.at(), two very different accessor methods. These inconsistencies make your results meaningless.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Iterator performance

    What is fastest depends on the implementation of the operator used to reach the data. A dumb for loop will usually be fastest, provided that the reaching operator (QList::at() in the first case, next() and * operator in remaining cases) doesn't introduce any unnecessary overhead.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    131
    Thanks
    11
    Thanked 16 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Iterator performance

    The good about the iterators is not the performance, but the save and easy use and the good readability (well except for the stl style imho).

    Here the Benchmark from Qt for different iteration styles: http://labs.qt.nokia.com/2009/01/23/...g-efficiently/ I personally prefer the foreach macro, because most time its enough and its very simple to use.

  5. #5
    Join Date
    Jan 2011
    Posts
    34
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Iterator performance

    Quote Originally Posted by SixDegrees View Post
    Note, also, that your tests are not valid. In the STL case, you call list.end() on each iteration of the loop, incurring an unnecessary function call.
    Tried to use
    Qt Code:
    1. QList<int>::iterator i;
    2. QList<int>::iterator end = list.end();
    3. for(i = list.begin(); i != end; ++i)
    4. sum += *i;
    To copy to clipboard, switch view to plain text mode 
    and processing time decreased to 84 ms. But if try
    Qt Code:
    1. for(int i = 0; i < list.size(); i++)
    To copy to clipboard, switch view to plain text mode 
    or
    Qt Code:
    1. int size = list.size();
    2. for(int i = 0; i < size; i++)
    To copy to clipboard, switch view to plain text mode 
    results are the same. It seems that compiler optimizes this function call.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Iterator performance

    What if you declare size as "const int"? Does it change anything?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Jan 2011
    Posts
    34
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Iterator performance

    Quote Originally Posted by wysota View Post
    What if you declare size as "const int"? Does it change anything?
    The same time - 84 ms.

  8. #8
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Iterator performance

    So a well structured stl-style iterator is just as fast as the simplest method ?

  9. #9
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Iterator performance

    Most important question is how did you build it?
    Is it in release mode or in debug mode? I suspect that in debug mode (most developers are using it as default)!
    I wouldn't be surprised if results in alternative build mode would be much different.

  10. #10
    Join Date
    Jan 2011
    Posts
    34
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Iterator performance

    Quote Originally Posted by MarekR22 View Post
    Most important question is how did you build it?
    Is it in release mode or in debug mode? I suspect that in debug mode (most developers are using it as default)!
    I wouldn't be surprised if results in alternative build mode would be much different.
    It was built in release mode.

Similar Threads

  1. pointer and iterator
    By mickey in forum General Programming
    Replies: 6
    Last Post: 3rd February 2008, 22:24
  2. QLinkedList iterator
    By ^NyAw^ in forum Qt Programming
    Replies: 8
    Last Post: 18th October 2007, 16:15
  3. iterator
    By mickey in forum General Programming
    Replies: 6
    Last Post: 23rd May 2006, 10:28
  4. ::iterator
    By mickey in forum General Programming
    Replies: 2
    Last Post: 20th March 2006, 22:05
  5. convert iterator
    By mickey in forum General Programming
    Replies: 8
    Last Post: 20th March 2006, 21:59

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.