diff options
Diffstat (limited to 'tests/benchmarks/corelib/tools/qhash')
-rw-r--r-- | tests/benchmarks/corelib/tools/qhash/main.cpp (renamed from tests/benchmarks/corelib/tools/qhash/qhash_string.cpp) | 95 | ||||
-rw-r--r-- | tests/benchmarks/corelib/tools/qhash/main.h (renamed from tests/benchmarks/corelib/tools/qhash/qhash_string.h) | 16 | ||||
-rw-r--r-- | tests/benchmarks/corelib/tools/qhash/outofline.cpp | 30 | ||||
-rw-r--r-- | tests/benchmarks/corelib/tools/qhash/paths_small_data.txt (renamed from tests/benchmarks/corelib/tools/qhash/data.txt) | 0 | ||||
-rw-r--r-- | tests/benchmarks/corelib/tools/qhash/qhash.pro | 2 |
5 files changed, 88 insertions, 55 deletions
diff --git a/tests/benchmarks/corelib/tools/qhash/qhash_string.cpp b/tests/benchmarks/corelib/tools/qhash/main.cpp index 874a0c543a..6f3228d0eb 100644 --- a/tests/benchmarks/corelib/tools/qhash/qhash_string.cpp +++ b/tests/benchmarks/corelib/tools/qhash/main.cpp @@ -1,8 +1,7 @@ /**************************************************************************** ** ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). -** All rights reserved. -** Contact: Nokia Corporation (qt-info@nokia.com) +** Contact: http://www.qt-project.org/ ** ** This file is part of the test suite of the Qt Toolkit. ** @@ -35,38 +34,12 @@ ** ** ** +** ** $QT_END_LICENSE$ ** ****************************************************************************/ -/* - -//////////////////////////////////////////////////////////////////// - -This benchmark serves as reality check on the idea that hashing the complete -string is a good idea. - - Executive summary: It is not a good idea. - -//////////////////////////////////////////////////////////////////// - -********* Start testing of tst_QHash ********* -Config: Using QTest library 5.0.0, Qt 5.0.0 -PASS : tst_QHash::initTestCase() -RESULT : tst_QHash::qhash_qt4(): - 0.041 msecs per iteration (total: 85, iterations: 2048) -PASS : tst_QHash::qhash_qt4() -RESULT : tst_QHash::qhash_faster(): - 0.0122 msecs per iteration (total: 100, iterations: 8192) -PASS : tst_QHash::qhash_faster() -PASS : tst_QHash::cleanupTestCase() -Totals: 4 passed, 0 failed, 0 skipped - -//////////////////////////////////////////////////////////////////// - -*/ - -#include "qhash_string.h" +#include "main.h" #include <QFile> #include <QHash> @@ -81,51 +54,75 @@ class tst_QHash : public QObject Q_OBJECT private slots: + void qhash_qt4_data() { data(); } void qhash_qt4(); + void qhash_faster_data() { data(); } void qhash_faster(); + void javaString_data() { data(); } + void javaString(); private: - QString data(); + void data(); }; -const int N = 1000000; -extern double s; - ///////////////////// QHash ///////////////////// -QString tst_QHash::data() +void tst_QHash::data() { - QFile file("data.txt"); - file.open(QIODevice::ReadOnly); - return QString::fromLatin1(file.readAll()); + QFile smallPathsData("paths_small_data.txt"); + smallPathsData.open(QIODevice::ReadOnly); + + QTest::addColumn<QStringList>("items"); + QTest::newRow("paths-small") + << QString::fromLatin1(smallPathsData.readAll()).split(QLatin1Char('\n')); } void tst_QHash::qhash_qt4() { - QStringList items = data().split(QLatin1Char('\n')); + QFETCH(QStringList, items); + QStringList realitems = items; // for copy/paste ease between benchmarks QHash<QString, int> hash; - + QBENCHMARK { - for (int i = 0, n = items.size(); i != n; ++i) { - hash[items.at(i)] = i; + for (int i = 0, n = realitems.size(); i != n; ++i) { + hash[realitems.at(i)] = i; } } } void tst_QHash::qhash_faster() { - QList<String> items; - foreach (const QString &s, data().split(QLatin1Char('\n'))) - items.append(s); + QFETCH(QStringList, items); QHash<String, int> hash; - + + QList<String> realitems; + foreach (const QString &s, items) + realitems.append(s); + QBENCHMARK { - for (int i = 0, n = items.size(); i != n; ++i) { - hash[items.at(i)] = i; + for (int i = 0, n = realitems.size(); i != n; ++i) { + hash[realitems.at(i)] = i; } } } +void tst_QHash::javaString() +{ + QFETCH(QStringList, items); + QHash<JavaString, int> hash; + + QList<JavaString> realitems; + foreach (const QString &s, items) + realitems.append(s); + + QBENCHMARK { + for (int i = 0, n = realitems.size(); i != n; ++i) { + hash[realitems.at(i)] = i; + } + } +} + + QTEST_MAIN(tst_QHash) -#include "qhash_string.moc" +#include "main.moc" diff --git a/tests/benchmarks/corelib/tools/qhash/qhash_string.h b/tests/benchmarks/corelib/tools/qhash/main.h index 94f142914b..c4cf94e190 100644 --- a/tests/benchmarks/corelib/tools/qhash/qhash_string.h +++ b/tests/benchmarks/corelib/tools/qhash/main.h @@ -1,8 +1,7 @@ /**************************************************************************** ** ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). -** All rights reserved. -** Contact: Nokia Corporation (qt-info@nokia.com) +** Contact: http://www.qt-project.org/ ** ** This file is part of the QtTest module of the Qt Toolkit. ** @@ -35,6 +34,7 @@ ** ** ** +** ** $QT_END_LICENSE$ ** ****************************************************************************/ @@ -50,3 +50,15 @@ struct String : QString QT_BEGIN_NAMESPACE uint qHash(const String &); QT_END_NAMESPACE + + +struct JavaString : QString +{ + JavaString() {} + JavaString(const QString &s) : QString(s) {} +}; + +QT_BEGIN_NAMESPACE +uint qHash(const JavaString &); +QT_END_NAMESPACE + diff --git a/tests/benchmarks/corelib/tools/qhash/outofline.cpp b/tests/benchmarks/corelib/tools/qhash/outofline.cpp index 11a5f9e733..162c604a35 100644 --- a/tests/benchmarks/corelib/tools/qhash/outofline.cpp +++ b/tests/benchmarks/corelib/tools/qhash/outofline.cpp @@ -1,8 +1,7 @@ /**************************************************************************** ** ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). -** All rights reserved. -** Contact: Nokia Corporation (qt-info@nokia.com) +** Contact: http://www.qt-project.org/ ** ** This file is part of the QtTest module of the Qt Toolkit. ** @@ -35,11 +34,12 @@ ** ** ** +** ** $QT_END_LICENSE$ ** ****************************************************************************/ -#include "qhash_string.h" +#include "main.h" static void doHash(const unsigned short *p, uint &h) { @@ -87,4 +87,28 @@ uint qHash(const String &str) return h; } +// The Java's hashing algorithm for strings is a variation of D. J. Bernstein +// hashing algorithm appeared here http://cr.yp.to/cdb/cdb.txt +// and informally known as DJB33XX - DJB's 33 Times Xor. +// Java uses DJB31XA, that is, 31 Times Add. +// The original algorithm was a loop around "(h << 5) + h ^ c", +// which is indeed "h * 33 ^ c"; it was then changed to +// "(h << 5) - h ^ c", so "h * 31 ^ c", and the XOR changed to a sum: +// "(h << 5) - h + c", which can save some assembly instructions. +// Still, we can avoid writing the multiplication as "(h << 5) - h" +// -- the compiler will turn it into a shift and an addition anyway +// (for instance, gcc 4.4 does that even at -O0). +uint qHash(const JavaString &str) +{ + const unsigned short *p = (unsigned short *)str.constData(); + const int len = str.size(); + + uint h = 0; + + for (int i = 0; i < len; ++i) + h = 31 * h + p[i]; + + return h; +} + QT_END_NAMESPACE diff --git a/tests/benchmarks/corelib/tools/qhash/data.txt b/tests/benchmarks/corelib/tools/qhash/paths_small_data.txt index d5acd28820..d5acd28820 100644 --- a/tests/benchmarks/corelib/tools/qhash/data.txt +++ b/tests/benchmarks/corelib/tools/qhash/paths_small_data.txt diff --git a/tests/benchmarks/corelib/tools/qhash/qhash.pro b/tests/benchmarks/corelib/tools/qhash/qhash.pro index 51d3e7f253..40f661c116 100644 --- a/tests/benchmarks/corelib/tools/qhash/qhash.pro +++ b/tests/benchmarks/corelib/tools/qhash/qhash.pro @@ -1,5 +1,5 @@ TARGET = tst_hash QT = core testlib INCLUDEPATH += . -SOURCES += qhash_string.cpp outofline.cpp +SOURCES += main.cpp outofline.cpp CONFIG += release |