Speed up sorting of dir entries when sorted by date

QDateTime will attempt to convert unknown types of date to UTC time, which isn't
exactly a fast process. As we don't care about local timezones in the process of
sorting (as this is purely for ordering, not display to the end user), we can
force the dates to use UTC time, avoiding the unnecessary local timezone lookup.

This also adds a benchmark covering this case.

Benchmark results, Qt 5:
 - before: 11, 489ms
 - after: 273ms

Qt 4.8:
 - before: 20, 848ms
 - after: 278ms

Change-Id: I87fa6260e820b5b172d3306ff395dafe767c33ff
Reported-by: Thomas Perl <m@thp.io>
Reviewed-by: Alberto Mardegan <mardy@users.sourceforge.net>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Robin Burchell 2012-08-15 13:37:15 +02:00 committed by Qt by Nokia
parent f3fea329fd
commit b9ef4a9c30
2 changed files with 25 additions and 2 deletions

View File

@ -232,9 +232,20 @@ bool QDirSortItemComparator::operator()(const QDirSortItem &n1, const QDirSortIt
| (qt_cmp_si_sort_flags & QDir::Type);
switch (sortBy) {
case QDir::Time:
r = f1->item.lastModified().secsTo(f2->item.lastModified());
case QDir::Time: {
QDateTime firstModified = f1->item.lastModified();
QDateTime secondModified = f2->item.lastModified();
// QDateTime by default will do all sorts of conversions on these to
// find timezones, which is incredibly expensive. As we aren't
// presenting these to the user, we don't care (at all) about the
// local timezone, so force them to UTC to avoid that conversion.
firstModified.setTimeSpec(Qt::UTC);
secondModified.setTimeSpec(Qt::UTC);
r = firstModified.secsTo(secondModified);
break;
}
case QDir::Size:
r = int(qBound<qint64>(-1, f2->item.size() - f1->item.size(), 1));
break;

View File

@ -155,6 +155,18 @@ private slots:
}
}
void sorted_byTime() {
QDir testdir(QDir::tempPath() + QLatin1String("/test_speed"));
testdir.setSorting(QDir::Time);
testdir.setFilter(QDir::AllEntries | QDir::System | QDir::Hidden);
QBENCHMARK {
QStringList fileList = testdir.entryList(QDir::NoFilter, QDir::Time);
foreach (const QString &filename, fileList) {
}
}
}
void sizeSpeedWithoutFilterLowLevel() {
QDir testdir(QDir::tempPath() + QLatin1String("/test_speed"));
#ifdef Q_OS_WIN