Results 1 to 7 of 7

Thread: Why is QMap (and also std::map) significantly slower than MS .Net Dictionary class?

  1. #1
    Join Date
    May 2011
    Posts
    46
    Thanks
    5

    Default Why is QMap (and also std::map) significantly slower than MS .Net Dictionary class?

    I have the following C++ program:
    Qt Code:
    1. #include <map>
    2. #include <string>
    3. #include <QDebug>
    4. #include <QMap>
    5. #include <QElapsedTimer>
    6.  
    7. using namespace std;
    8.  
    9. int main()
    10. {
    11. QElapsedTimer t;
    12.  
    13. qint64 e;
    14. const int COUNT = 1000000;
    15.  
    16. // using std::map
    17. t.start();
    18. map<int,string> m;
    19. for(int i = 0; i < COUNT; i++)
    20. m[i] = "HI";
    21.  
    22. e = t.elapsed();
    23. qDebug() << e;
    24.  
    25. // using Map
    26. t.restart();
    27. QMap<int,string> m2;
    28. for(int i = 0; i < COUNT; i++)
    29. m2[i] = "HI";
    30. e = t.elapsed();
    31. qDebug() << e;
    32.  
    33. return 0;
    34. }
    To copy to clipboard, switch view to plain text mode 
    Compiled in release mode, the std::map version gives me a result of 208 ms, while the QMap version gives me a result of 220 ms.
    But the .Net version is much faster
    Qt Code:
    1. using System;
    2. using System.Collections.Generic;
    3. using System.Diagnostics;
    4. internal class Program
    5. {
    6. private static void Main()
    7. {
    8. Stopwatch w = Stopwatch.StartNew();
    9. Dictionary<int, string> dic = new Dictionary<int, string>();
    10. long e;
    11. const int COUNT = 1000000;
    12.  
    13. for (int i = 0; i < COUNT; i++)
    14. {
    15. dic[i] = "HI";
    16. }
    17. w.Stop();
    18. e = w.ElapsedMilliseconds;
    19. Console.WriteLine(e);
    20. }
    21. }
    To copy to clipboard, switch view to plain text mode 
    This gives me a result of 60 ms!
    Am I doing something wrong? Or the result is actually normal?

  2. #2
    Join Date
    Jan 2011
    Posts
    14
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Why is QMap (and also std::map) significantly slower than MS .Net Dictionary clas

    Replace QMap by QHash : http://doc.qt.nokia.com/4.7/qhash.html#details
    It's perhaps more similar of Dictionnary

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

    Default Re: Why is QMap (and also std::map) significantly slower than MS .Net Dictionary clas

    Kangs is correct. QMap and std::map both use binary-tree based structures. QHash (there is no STL analogue) uses an actual hash table that will be limited predominantly by the hashing function, which should be very fast for int-type keys.

    Note, also, that your choice of populating the containers - a series of sequential integers used as keys - guarantees worst-case insertion and retrieval performance in many implementations, as you wind up with tree as deep as the number of items, resulting in O(n) performance. A more realistic test would be to generate a set of random keys outside the timing loop, then use these to populate the container.

    You should also test retrieval speed. It is common to populate such containers once, then retrieve information from them many times, so retrieval speed is often the more important metric. In fact, there isn't much point in storing data in a container if it will never be accessed, or will only be accessed once.

  4. #4
    Join Date
    May 2011
    Posts
    46
    Thanks
    5

    Default Re: Why is QMap (and also std::map) significantly slower than MS .Net Dictionary clas

    Quote Originally Posted by Kangs View Post
    Replace QMap by QHash : http://doc.qt.nokia.com/4.7/qhash.html#details
    It's perhaps more similar of Dictionnary
    I replaced QMap by QHash, and the result boost to 112 ms instead of 220 ms using QMap, but still, why is the .Net version run so much faster (60 ms)??

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

    Default Re: Why is QMap (and also std::map) significantly slower than MS .Net Dictionary clas

    Different hashing algorithm. As already noted, you're only testing insertion, so the only difference will be due to how the value is hashed, with some smaller difference attributable to the precise structure of the hash bins.

    If you want more details, you'll need the code for both algorithms. Honestly, I don't see much difference. Are you really going to care if a million inserts takes 0.05 seconds longer? Particularly when it's much more likely to be retrieval rather than insertion speed which is more important?

  6. #6
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Why is QMap (and also std::map) significantly slower than MS .Net Dictionary clas

    I agree with SixDegrees. If you want to measure container performance, then test only this, with random data. There is almost no point in measuring the container allocation time, construct a test case which is closer to real life - test the insertion and retrieval time with random data instead (if you really need to).
    In real-life application the performance can be killed by, for instance, slow window manager or wrong/slow algorithm or thousands other things. Fast dictionary wont help you in that case.

  7. #7
    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: Why is QMap (and also std::map) significantly slower than MS .Net Dictionary clas

    I suspect that problem is also in use of string "HI". Note that for .net this string is representing object at compile time and no conversion is required. In Qt/C++ code this string is a C-string which is converted to QString or std::string and this will give performance penalty.
    Try define a const value of std::string or QString before start measurement.

Similar Threads

  1. Qt 4.6.3 slower than 4.6.2
    By Nik8768 in forum Installation and Deployment
    Replies: 3
    Last Post: 19th July 2010, 12:13
  2. dictionary done))
    By king-gideon in forum Qt Programming
    Replies: 5
    Last Post: 29th July 2009, 02:06
  3. help: designing a simple dictionary in Qt
    By king-gideon in forum Qt Programming
    Replies: 17
    Last Post: 17th July 2009, 23:00
  4. dictionary databases
    By rishiraj in forum Newbie
    Replies: 1
    Last Post: 5th January 2009, 08:30
  5. [java] write and read from file a dictionary
    By mickey in forum General Programming
    Replies: 1
    Last Post: 15th December 2008, 17:54

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
  •  
Qt is a trademark of The Qt Company.