简体   繁体   English

为什么Python比C ++排序更快

[英]Why is Python sort faster than that of C++

Sorting a list of ints in python 3 seems to be faster than sorting an array of ints in C++. 在python 3中对int列表进行排序似乎比对C ++中的int数组进行排序更快。 Below is the code for 1 python program and 2 C++ programs that I used for the test. 以下是我用于测试的1个python程序和2个c + +程序的代码。 Any reason why the C++ programs are slower? C ++程序变慢的任何原因? It doesn't make sense to me. 对我来说这没有意义。

----- Program 1 - python 3.4 ----- -----程序1-python 3.4 -----

from time import time

x = 10000
y = 1000

start = time()

for _ in range(y):
    a = list(range(x))
    a.reverse()
    a.sort()

print(round(time() - start, 2), 'seconds')

----- Program 2 - c++ using sort from algorithm ------ -----程序2-c ++使用排序算法------

using namespace std;
#include <iostream>
#include <algorithm>

int main(){

    int x = 10000; 
    int y = 1000;
    int b[10000];

    cout << "start" << endl;

    for (int j = 0; j < y; j++){
        for (int i = 0; i < x; i++){
            b[i] = x - i;
        } // still slower than python with this clause taken out

        sort(b, b + x); // regular sort

    }

    cout << "done";
    system("pause");
}

----- Program 3 - c++ using hand written merge sort ------ -----程序3-使用手写合并排序的c ++ ------

using namespace std;
#include <iostream>

void merge(int * arr, int *temp, int first_start, int second_start, int      second_finish){

    int a1 = first_start, b1 = second_start, r = 0;

    while (a1 < second_start && b1 < second_finish){

        if (arr[a1] < arr[b1]){
            temp[r] = arr[a1];
            a1++; r++;
        }
        else {
            temp[r] = arr[b1];
            b1++; r++;
        }
    }
    if (a1 < second_start){

        while (a1 < second_start){
            temp[r] = arr[a1];
            a1++; r++;
        }
    }

    else {
        while (b1 < second_finish){
            temp[r] = arr[b1];
            b1++; r++;
        }
    }

    for (int i = first_start; i < second_finish; i++){
        arr[i] = temp[i - first_start];
    }
}

void merge_sort(int *a, int a_len, int *temp){
    int c = 1, start = 0;
    while (c < a_len){
        while (start + c * 2 < a_len){
            merge(a, temp, start, start + c, start + c * 2);
            start += c * 2;
        }
        if (start + c <= a_len){
            merge(a, temp, start, start + c, a_len);
        }
        c *= 2; start = 0;
    } 
}

int main(){

    int x = 10000; // size of array to be sorted
    int y = 1000; // number of times to sort it
    int b[10000], temp[10000]; 

    cout << "start" << endl;

    for (int j = 0; j < y; j++){

        for (int i = 0; i < x; i++){

            b[i] = x - i; // reverse sorted array (even with this assignment taken out still runs slower than python)

        }

        merge_sort(b, x, temp);

    }

    cout << "done";
    system("pause");
}

The core reason is no doubt timsort -- http://en.wikipedia.org/wiki/Timsort -- first conceived by Tim Peters for Python though now also in some Java VMs (for non-primitives only). 毫无疑问,核心原因是timsort - http://en.wikipedia.org/wiki/Timsort-由Tim Peters最初为Python构想,但现在也在某些Java VM中使用(仅适用于非基本体)。

It's a truly amazing algorithm and you can find a C++ implementation at https://github.com/swenson/sort for example. 这是一个非常了不起的算法,例如,您可以在https://github.com/swenson/sort上找到C ++实现。

Lesson to retain: the proper architecture and algorithms can let you run circles around supposedly-faster languages if the latter are using less-perfect A & As!-) So, if you have really big problems to solve, deal with determining perfect architecture and algorithms first -- the language and optimizations within it are inevitably lower-priority issues. 保留的教训:如果后者使用的是性能较差的A&As,那么正确的体系结构算法可以让您绕开更快的语言 !-)因此,如果您有很大的问题要解决,则可以确定完美的体系结构和解决方案。首先是算法-语言和其中的优化不可避免地是较低优先级的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM