summaryrefslogtreecommitdiffstats
path: root/src/corelib/tools/qtimezoneprivate.cpp
diff options
context:
space:
mode:
authorDavid Faure <david.faure@kdab.com>2018-04-22 22:26:10 +0200
committerDavid Faure <david.faure@kdab.com>2018-04-23 06:48:12 +0000
commit46ec170d2be226c41b910a3709434e60c4310e6f (patch)
tree9be72b046de65b22d4e353abfddb5413a2d41a9b /src/corelib/tools/qtimezoneprivate.cpp
parentc782ed3c48b31dc38b728947270df3fc3f3a7626 (diff)
Speed up QTimeZone::isTimeZoneIdAvailable by a factor 43
Creating and sorting a list of bytearrays just to check if one entry is present, is really overkill. By adding a new virtual method isTimeZoneIdAvailable() in the backend classes, we can do this much more efficiently. Implemented for Utc and Tz backends, the others fall back to the slow way. The new benchmark shows, in release mode: Before: 43 msecs per iteration (total: 86, iterations: 2) After: 1.1 msecs per iteration (total: 73, iterations: 64) Change-Id: Ic0d79a41d74e2ce6aa088fa7986c41d33902c36b Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib/tools/qtimezoneprivate.cpp')
-rw-r--r--src/corelib/tools/qtimezoneprivate.cpp18
1 files changed, 18 insertions, 0 deletions
diff --git a/src/corelib/tools/qtimezoneprivate.cpp b/src/corelib/tools/qtimezoneprivate.cpp
index 1a5135f103..2b6c2ea6a5 100644
--- a/src/corelib/tools/qtimezoneprivate.cpp
+++ b/src/corelib/tools/qtimezoneprivate.cpp
@@ -486,6 +486,13 @@ QByteArray QTimeZonePrivate::systemTimeZoneId() const
return QByteArray();
}
+bool QTimeZonePrivate::isTimeZoneIdAvailable(const QByteArray& ianaId) const
+{
+ // Fall-back implementation, can be made faster in subclasses
+ const QList<QByteArray> tzIds = availableTimeZoneIds();
+ return std::binary_search(tzIds.begin(), tzIds.end(), ianaId);
+}
+
QList<QByteArray> QTimeZonePrivate::availableTimeZoneIds() const
{
return QList<QByteArray>();
@@ -864,6 +871,17 @@ QByteArray QUtcTimeZonePrivate::systemTimeZoneId() const
return utcQByteArray();
}
+bool QUtcTimeZonePrivate::isTimeZoneIdAvailable(const QByteArray &ianaId) const
+{
+ for (int i = 0; i < utcDataTableSize; ++i) {
+ const QUtcData *data = utcData(i);
+ if (utcId(data) == ianaId) {
+ return true;
+ }
+ }
+ return false;
+}
+
QList<QByteArray> QUtcTimeZonePrivate::availableTimeZoneIds() const
{
QList<QByteArray> result;