Guest User

benchmark.cc

a guest
Feb 19th, 2013
464
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.16 KB | None | 0 0
  1. /* Copyright 2013 Olivier Goffart <[email protected]>
  2. http://woboq.com/blog/qmap_qhash_benchmark.html
  3. */
  4. #include <QtCore/QtCore>
  5. #include <unordered_map>
  6. #ifndef CONTAINER
  7. #error CONTAINER must be defined to QMap, QHash, std::map or std::unordered_map
  8. #endif
  9. namespace std{
  10. /* std::hash specialization for QString so it can be used
  11. * as a key in std::unordered_map */
  12. template<class Key> struct hash;
  13. template<> struct hash<QString> {
  14. typedef QString Key;
  15. typedef uint result_type;
  16. inline uint operator()(const QString &s) const { return qHash(s); }
  17. };
  18. }
  19. int main(int argc, char **argv) {
  20. if (argc < 2)
  21. qFatal(""Missing number of element to add"");
  22. QByteArray a = argv[1];
  23. uint num = a.toUInt();
  24. // creates an array of random keys
  25. QVector<QString> strs(num);
  26. for (int i=0; i < num; ++i)
  27. strs[i] = qvariant_cast<QString>(qrand());
  28. CONTAINER<QString, QString> c;
  29. for (uint i = 0; i < num; ++i) {
  30. QString &k = strs[i];
  31. c[k] = QString::number(i);
  32. }
  33. quint64 it = 0;
  34. const QString *arr = strs.constData();
  35. QElapsedTimer t;
  36. t.start();
  37. while (t.elapsed() < 1000) {
  38. const QString &k = arr[(++it)*797%num];
  39. c[k]; // perform a lookup
  40. }
  41. qDebug() << it/1000;
  42. }
Advertisement
Add Comment
Please, Sign In to add comment