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>
This commit is contained in:
Marc Mutz 2013-09-11 23:10:18 +02:00 committed by The Qt Project
parent 12bd604f24
commit 48586b2bac

View File

@ -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