Add a string hash implementation similar to the one in Java.
This uses a similar runtime to the approach of sampling part of the string, with
the benefit that it doesn't reduce the sampling to subsections of the string.
Ironically, Java used to only sample parts of the string as well, but found that
it produced too many collisions with certain string types, so they moved to use
this method.
RESULT : tst_QHash::qhash_qt4():
0.0537 msecs per iteration (total: 110, iterations: 2048)
PASS : tst_QHash::qhash_qt4()
RESULT : tst_QHash::qhash_faster():
0.015 msecs per iteration (total: 62, iterations: 4096)
PASS : tst_QHash::qhash_faster()
RESULT : tst_QHash::javaString():
0.016 msecs per iteration (total: 66, iterations: 4096)
Change-Id: Icb5da341ab6445163f4217650a0bdb3903e50210
Reviewed-by: hjk <qthjk@ovi.com>
bb10
parent
03700a293e
commit
8060dd3c42
|
|
@ -87,4 +87,17 @@ uint qHash(const String &str)
|
|||
return h;
|
||||
}
|
||||
|
||||
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
|
||||
|
|
|
|||
|
|
@ -83,6 +83,7 @@ class tst_QHash : public QObject
|
|||
private slots:
|
||||
void qhash_qt4();
|
||||
void qhash_faster();
|
||||
void javaString();
|
||||
|
||||
private:
|
||||
QString data();
|
||||
|
|
@ -126,6 +127,21 @@ void tst_QHash::qhash_faster()
|
|||
}
|
||||
}
|
||||
|
||||
void tst_QHash::javaString()
|
||||
{
|
||||
QList<JavaString> items;
|
||||
foreach (const QString &s, data().split(QLatin1Char('\n')))
|
||||
items.append(s);
|
||||
QHash<JavaString, int> hash;
|
||||
|
||||
QBENCHMARK {
|
||||
for (int i = 0, n = items.size(); i != n; ++i) {
|
||||
hash[items.at(i)] = i;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
QTEST_MAIN(tst_QHash)
|
||||
|
||||
#include "qhash_string.moc"
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue