C++ 为什么反向排序比重新排序快

C++ 为什么反向排序比重新排序快,c++,sorting,C++,Sorting,我写了这样一个测试- std::vector<int> test_vector; for (int i = 0; i < 100000000; ++i) { test_vector.push_back(i);

我写了这样一个测试-

std::vector<int> test_vector;                                                        

for (int i = 0; i < 100000000; ++i) {                                                
    test_vector.push_back(i);                                                        
}                                                                                    

QElapsedTimer timer;                                                                 
timer.start();                                                                       

std::sort(test_vector.begin(),test_vector.end(), [](int a, int b) { return a < b; });

qDebug() << "The slow operation took" << timer.elapsed() << "milliseconds";          
qDebug() << "The slow operation took" << timer.nsecsElapsed() << "nanoseconds";      
但是当我改变的时候

std::sort(test_vector.begin(),test_vector.end(), [](int a, int b) { return a > b; });
结果

The slow operation took 2867 milliseconds
The slow operation took 2867591800 nanoseconds
我在Qt_5_12_3_MinGW_64_位版本上进行了测试,不明白为什么反向排序比重新排序更快

决心 我在Qt_5_12_3_MSVC2017_64位上测试了相同的示例,问题已经解决,问题出现在MinGW_64中

但是,我仍然有一个问题,为什么如果我把向量分成一个feed,所有元素都是10

#include <chrono>
#include<iostream>
#include <vector>
#include <algorithm>



    int main() {

        std::vector<int> test_vector;

        for (int i = 0; i < 100000000; ++i) {
            test_vector.push_back(10);
        }

        auto begin = chrono::high_resolution_clock::now();

        std::sort(test_vector.begin(), test_vector.end(), [](int a, int b) { return a < b; });


        auto end = std::chrono::high_resolution_clock::now();
        auto dur = end - begin;
        auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(dur).count();
        std::cout << ms << endl;


        return 0;
    }
#包括
#包括
#包括
#包括
int main(){
std::向量测试\向量;
对于(int i=0;i<100000000;++i){
测试向量。推回(10);
}
自动开始=计时::高分辨率时钟::现在();
排序(test_vector.begin(),test_vector.end(),[](inta,intb){返回astd::这是两个不同的测试吗,还是您在同一次运行中一个接一个地执行这两个测试?当数据已经排序时,快速排序的最坏情况效率为O(n^2)。如果您的标准库实现使用此算法,则结果是可以解释的(注意-从C++11中要求复杂性最多为O(n*logn),所以这不是现代C++的正确解释。)@François Andrieux我一次测试一个,100次,还有,你是如何构建代码的?使用了什么优化级别,等等,@Yksisarvinen我正在QT creator QT_5_12_3_MinGW_64_位版本上测试代码,在这两种情况下,我都在Release Build中使用此代码,这是两个不同的测试,还是你在s中一个接一个地进行这两个测试ame run?当数据已经排序时,快速排序的最坏情况效率为O(n^2)。如果标准库实现使用此算法,结果是可以解释的(注意-从C++11开始,复杂性要求最多为O(n*logn),因此这不是现代C++的正确解释)@François Andrieux我一次测试了一个,100次,还有,你是如何构建代码的?使用了什么优化级别,等等。@Yksisarvinen我正在QT creator QT_5_12_3_MinGW_64_位版本上测试代码,在这两种情况下,我都在发布版本中使用此代码
#include <chrono>
#include<iostream>
#include <vector>
#include <algorithm>



    int main() {

        std::vector<int> test_vector;

        for (int i = 0; i < 100000000; ++i) {
            test_vector.push_back(10);
        }

        auto begin = chrono::high_resolution_clock::now();

        std::sort(test_vector.begin(), test_vector.end(), [](int a, int b) { return a < b; });


        auto end = std::chrono::high_resolution_clock::now();
        auto dur = end - begin;
        auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(dur).count();
        std::cout << ms << endl;


        return 0;
    }
for (int i = 0; i < 100000000; ++i) {
        test_vector.push_back(i);
    }