Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Copyright 2013 Olivier Goffart <[email protected]>
- http://woboq.com/blog/qmap_qhash_benchmark.html
- */
- #include <QtCore/QtCore>
- #include <unordered_map>
- #ifndef CONTAINER
- #error CONTAINER must be defined to QMap, QHash, std::map or std::unordered_map
- #endif
- namespace std{
- /* std::hash specialization for QString so it can be used
- * as a key in std::unordered_map */
- template<class Key> struct hash;
- template<> struct hash<QString> {
- typedef QString Key;
- typedef uint result_type;
- inline uint operator()(const QString &s) const { return qHash(s); }
- };
- }
- int main(int argc, char **argv) {
- if (argc < 2)
- qFatal(""Missing number of element to add"");
- QByteArray a = argv[1];
- uint num = a.toUInt();
- // creates an array of random keys
- QVector<QString> strs(num);
- for (int i=0; i < num; ++i)
- strs[i] = qvariant_cast<QString>(qrand());
- CONTAINER<QString, QString> c;
- for (uint i = 0; i < num; ++i) {
- QString &k = strs[i];
- c[k] = QString::number(i);
- }
- quint64 it = 0;
- const QString *arr = strs.constData();
- QElapsedTimer t;
- t.start();
- while (t.elapsed() < 1000) {
- const QString &k = arr[(++it)*797%num];
- c[k]; // perform a lookup
- }
- qDebug() << it/1000;
- }
Advertisement
Add Comment
Please, Sign In to add comment