diff options
author | Mike FABIAN <mike.fabian@basyskom.de> | 2010-12-02 15:56:45 +0100 |
---|---|---|
committer | Mike FABIAN <mike.fabian@basyskom.de> | 2010-12-02 17:27:22 +0100 |
commit | 632b28884c3aa9387128d10b8101fb98988b6ccb (patch) | |
tree | 730097a22b5dd9d42f757b02832ac371f7625c7e /src/corelib/i18n | |
parent | 69015d449d8210c02b424f235e745f3e0584efeb (diff) |
Fixes: NB#206357, Calling MLocale::formatDateTime() takes over 7msec
RevBy: John Tapsell, Berthold Krevert
Details:
Speed improvement is about a factor of 7:
With Cache:
RESULT : Pt_MCalendar::benchmarkFormatDateTime():
0.074 msecs per iteration (total: 76, iterations: 1024)
RESULT : Pt_MCalendar::benchmarkFormatDateTime():
0.069 msecs per iteration (total: 71, iterations: 1024)
Without cache:
RESULT : Pt_MCalendar::benchmarkFormatDateTime():
0.50 msecs per iteration (total: 65, iterations: 128)
RESULT : Pt_MCalendar::benchmarkFormatDateTime():
0.53 msecs per iteration (total: 69, iterations: 128)
Diffstat (limited to 'src/corelib/i18n')
-rw-r--r-- | src/corelib/i18n/mlocale.cpp | 49 | ||||
-rw-r--r-- | src/corelib/i18n/mlocale_p.h | 1 |
2 files changed, 32 insertions, 18 deletions
diff --git a/src/corelib/i18n/mlocale.cpp b/src/corelib/i18n/mlocale.cpp index 71fb44f5..5c569f11 100644 --- a/src/corelib/i18n/mlocale.cpp +++ b/src/corelib/i18n/mlocale.cpp @@ -488,18 +488,27 @@ QString MLocalePrivate::icuFormatString(MLocale::DateType dateType, icu::UnicodeString icuFormatString; static_cast<SimpleDateFormat *>(df)->toPattern(icuFormatString); icuFormatQString = MIcuConversions::unicodeStringToQString(icuFormatString); - delete df; } return icuFormatQString; } #endif #ifdef HAVE_ICU +static QCache<QString, icu::DateFormat> _dateFormatCache; + icu::DateFormat *MLocalePrivate::createDateFormat(MLocale::DateType dateType, MLocale::TimeType timeType, MLocale::CalendarType calendarType, MLocale::TimeFormat24h timeFormat24h) const { + QString key = QString("%1_%2_%3_%4_%5") + .arg(dateType) + .arg(timeType) + .arg(calendarType) + .arg(timeFormat24h) + .arg(categoryName(MLocale::MLcTime)); + if (_dateFormatCache.contains(key)) + return _dateFormatCache.object(key); // Create calLocale which has the time pattern we want to use icu::Locale calLocale = MIcuConversions::createLocale( categoryName(MLocale::MLcTime), @@ -521,20 +530,21 @@ icu::DateFormat *MLocalePrivate::createDateFormat(MLocale::DateType dateType, // symbols with the public API static_cast<SimpleDateFormat *>(df)->adoptDateFormatSymbols(dfs); #endif - if (timeType == MLocale::TimeNone) - return df; - switch (timeFormat24h) { - case(MLocale::TwelveHourTimeFormat24h): - MLocalePrivate::dateFormatTo12h(df); - break; - case(MLocale::TwentyFourHourTimeFormat24h): - MLocalePrivate::dateFormatTo24h(df); - break; - case(MLocale::LocaleDefaultTimeFormat24h): - break; - default: - break; + if (timeType != MLocale::TimeNone) { + switch (timeFormat24h) { + case(MLocale::TwelveHourTimeFormat24h): + MLocalePrivate::dateFormatTo12h(df); + break; + case(MLocale::TwentyFourHourTimeFormat24h): + MLocalePrivate::dateFormatTo24h(df); + break; + case(MLocale::LocaleDefaultTimeFormat24h): + break; + default: + break; + } } + _dateFormatCache.insert(key, df); return df; } #endif @@ -2081,8 +2091,8 @@ QString MLocale::formatDateTime(const MCalendar &mcalendar, icu::DateFormat *df = d->createDateFormat(datetype, timetype, mcalendar.type(), d->_timeFormat24h); - df->format(*cal, resString, pos); - delete df; + if(df) + df->format(*cal, resString, pos); return MIcuConversions::unicodeStringToQString(resString); } #endif @@ -2531,8 +2541,11 @@ QDateTime MLocale::parseDateTime(const QString &dateTime, DateType dateType, mcalendar.type(), d->_timeFormat24h); icu::ParsePosition pos(0); - UDate parsedDate = df->parse(text, pos); - delete df; + UDate parsedDate; + if (df) + parsedDate = df->parse(text, pos); + else + return QDateTime(); UErrorCode status = U_ZERO_ERROR; icu::Calendar *cal = mcalendar.d_ptr->_calendar; diff --git a/src/corelib/i18n/mlocale_p.h b/src/corelib/i18n/mlocale_p.h index 74fd064b..45ba2a09 100644 --- a/src/corelib/i18n/mlocale_p.h +++ b/src/corelib/i18n/mlocale_p.h @@ -26,6 +26,7 @@ #include <QStringList> #include <QExplicitlySharedDataPointer> #include <QLocale> +#include <QCache> #ifdef HAVE_ICU #include <unicode/datefmt.h> |