From 4467ebc837aded07b0b0cbc3b34ce67f2c0e96f3 Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Wed, 17 Apr 2024 16:30:24 +0200 Subject: [PATCH] Add benchmarks for QDate::fromString() These reveal a roughly factor of six slow-down for two valid date formats and a roughly factor of twelve slow-down for an invalid one. Pick-to: 6.7 6.5 6.2 Task-number: QTBUG-124465 Change-Id: Ibd21e43d4c64aced33ba5b21e4602e0dc4fd7548 Reviewed-by: Mate Barany --- .../corelib/time/qdate/tst_bench_qdate.cpp | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/tests/benchmarks/corelib/time/qdate/tst_bench_qdate.cpp b/tests/benchmarks/corelib/time/qdate/tst_bench_qdate.cpp index 581c300d364..7dde3bf4265 100644 --- a/tests/benchmarks/corelib/time/qdate/tst_bench_qdate.cpp +++ b/tests/benchmarks/corelib/time/qdate/tst_bench_qdate.cpp @@ -4,6 +4,7 @@ #include #include #include +using namespace Qt::StringLiterals; class tst_QDate : public QObject { @@ -33,6 +34,9 @@ private Q_SLOTS: void addDays(); void addMonths(); void addYears(); + + void fromString_data(); + void fromString(); }; QList tst_QDate::daily(qint64 start, qint64 end) @@ -184,5 +188,28 @@ void tst_QDate::addYears() Q_UNUSED(store); } +void tst_QDate::fromString_data() +{ + QTest::addColumn("string"); + QTest::addColumn("format"); + QTest::addColumn("baseYear"); + + QTest::newRow("yyyyMMdd") << u"20240412"_s << u"yyyyMMdd"_s << 2000; + QTest::newRow("yyyy-MM-dd") << u"2024-04-12"_s << u"yyyy-MM-dd"_s << 2000; + QTest::newRow("YYYYMMDD") << u"20240412"_s << u"YYYYMMDD"_s << 2000; // Invalid, QTBUG-124465. +} + +void tst_QDate::fromString() +{ + QFETCH(const QString, string); + QFETCH(const QString, format); + QFETCH(const int, baseYear); + QDate date; + QBENCHMARK { + date = QDate::fromString(string, format, baseYear); + } + Q_UNUSED(date); +} + QTEST_MAIN(tst_QDate) #include "tst_bench_qdate.moc"