[clazy] [Bug 361691] Suggestion to turn string into QStringLiteral instead of QLatin1String
Stephen Kelly via KDE Bugzilla
bugzilla_noreply at kde.org
Wed Apr 13 22:20:46 BST 2016
https://bugs.kde.org/show_bug.cgi?id=361691
--- Comment #4 from Stephen Kelly <steveire at gmail.com> ---
I also tried with larger strings. The fact that the QLatin1String overload is
slower is suspect. It could be due to the test string being very small, but
that's what I expect is the primary use-case of QLatin1String.
#include <QtTest/QTest>
#include <QtCore/QDebug>
class StringBenchmark : public QObject
{
Q_OBJECT
public:
StringBenchmark(QObject* parent = nullptr) : QObject(parent)
{
for (auto i = 0; i < 1000000; ++i)
{
mTestString += QStringLiteral("Long string part %1 --").arg(i);
}
}
private slots:
void containsQStringLiteral() {
QBENCHMARK {
auto result = mTestString.contains(QStringLiteral("Long string part
557755"));
QVERIFY(result);
}
}
void containsQLatin1String() {
QBENCHMARK {
auto result = mTestString.contains(QLatin1String("Long string part
557755"));
QVERIFY(result);
}
}
void indexOfQStringLiteral() {
QBENCHMARK {
auto result = mTestString.indexOf(QStringLiteral("Long string part
557755"));
QCOMPARE(result, 14390520);
}
}
void indexOfQLatin1String() {
QBENCHMARK {
auto result = mTestString.indexOf(QLatin1String("Long string part
557755"));
QCOMPARE(result, 14390520);
}
}
private:
QString mTestString;
};
QTEST_MAIN(StringBenchmark)
#include "tester.moc"
********* Start testing of StringBenchmark *********
Config: Using QtTest library 5.4.2, Qt 5.4.2 (x86_64-little_endian-lp64 shared
(dynamic) release build; by GCC 5.2.1 20151010)
PASS : StringBenchmark::initTestCase()
PASS : StringBenchmark::containsQStringLiteral()
RESULT : StringBenchmark::containsQStringLiteral():
5.1 msecs per iteration (total: 83, iterations: 16)
PASS : StringBenchmark::containsQLatin1String()
RESULT : StringBenchmark::containsQLatin1String():
5.2 msecs per iteration (total: 84, iterations: 16)
PASS : StringBenchmark::indexOfQStringLiteral()
RESULT : StringBenchmark::indexOfQStringLiteral():
5.2 msecs per iteration (total: 84, iterations: 16)
PASS : StringBenchmark::indexOfQLatin1String()
RESULT : StringBenchmark::indexOfQLatin1String():
5.2 msecs per iteration (total: 84, iterations: 16)
PASS : StringBenchmark::cleanupTestCase()
Totals: 6 passed, 0 failed, 0 skipped, 0 blacklisted
********* Finished testing of StringBenchmark *********
--
You are receiving this mail because:
You are the assignee for the bug.
More information about the Unassigned-bugs
mailing list