diff options
author | Marc Mutz <marc.mutz@kdab.com> | 2013-09-11 23:10:18 +0200 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2013-09-13 11:47:06 +0200 |
commit | 48586b2bac623605e9f300f8e5380e4f4b484dab (patch) | |
tree | f3042d8bee7cc54b0733f12aa73dd93cc27da2b8 /src/testlib/qtestcase.cpp | |
parent | 12bd604f241d41d52ab3fde9f4a8f4f5c2f3fa6d (diff) |
QTest: use nth_element to calculate the median
Sorting is O(NlogN) complexity, while nth_element is linear.
Change-Id: Ic6596affe183494e87abe7bdaa7c9985f5b7cd58
Reviewed-by: Olivier Goffart <ogoffart@woboq.com>
Diffstat (limited to 'src/testlib/qtestcase.cpp')
-rw-r--r-- | src/testlib/qtestcase.cpp | 9 |
1 files changed, 5 insertions, 4 deletions
diff --git a/src/testlib/qtestcase.cpp b/src/testlib/qtestcase.cpp index 9d9fcced76..0cf2f3256b 100644 --- a/src/testlib/qtestcase.cpp +++ b/src/testlib/qtestcase.cpp @@ -1631,13 +1631,14 @@ QBenchmarkResult qMedian(const QList<QBenchmarkResult> &container) if (count == 1) return container.at(0); - QList<QBenchmarkResult> containerCopy = container; - std::sort(containerCopy.begin(), containerCopy.end()); - const int middle = count / 2; + QList<QBenchmarkResult> containerCopy = container; + const QList<QBenchmarkResult>::iterator begin = containerCopy.begin(), mid = begin + middle, end = containerCopy.end(); + std::nth_element(begin, mid, end); + // ### handle even-sized containers here by doing an aritmetic mean of the two middle items. - return containerCopy.at(middle); + return *mid; } struct QTestDataSetter |