Cython——使python速度与C相当,用Cython完成快排的小案例

Cython

cython官网: https://cython.readthedocs.io/
代码: https://github.com/Jintao-Huang/dnn_study/tree/master/cython_study/quicksort
视频讲解: https://www.bilibili.com/video/BV1MX4y1573k

代码

代码目录结构:
目录结构

quick_sort.cpp

template<typename T>
inline void _swap(T &a, T &b) {
    T c = a;
    a = b;
    b = c;
}

// 按low进行划分
template<typename T>
int partition(T arr[], int low, int high) {
    // 划分
    T pivot = arr[low];
    while (low < high) {
        while (low < high && pivot <= arr[high]) --high;
        arr[low] = arr[high];
        while (low < high && arr[low] <= pivot) ++low;
        arr[high] = arr[low];
    }
    arr[low] = pivot;
    return low;
}
// 快排[low, high] 闭区间
template<typename T>
void quick_sort(T arr[], int low, int high) {
    if (low < high) {
        // 用mid进行划分(避免顺序或逆序时最坏复杂度)
        int mid = (low + high) / 2;
        _swap(arr[low], arr[mid]);
        int pivot_pos = partition(arr, low, high);
        quick_sort(arr, low, pivot_pos - 1);
        quick_sort(arr, pivot_pos + 1, high);
    }
}

sort.pyx

# distutils: language=c++
from libcpp.vector cimport vector
cdef extern from "quick_sort.cpp":
    void _quick_sort "quick_sort"(int arr[], int low, int high)

def quick_sort(arr, int low, int high):
    cdef vector[int] temp = arr  # list
    _quick_sort(temp.data(), low, high)
    arr[:] = temp

setup.py

from setuptools import setup
from Cython.Build import cythonize

setup(
    ext_modules=cythonize("sort.pyx")
)

main.py

# 按low进行划分
def partition(arr, low, high):
    # 划分
    pivot = arr[low]
    while (low < high):
        while low < high and pivot <= arr[high]:
            high -= 1
        arr[low] = arr[high]
        while low < high and arr[low] <= pivot:
            low += 1
        arr[high] = arr[low]

    arr[low] = pivot
    return low


# 快排[low, high] 闭区间
def quick_sort(arr, low, high):
    if low < high:
        # 用mid进行划分(避免顺序或逆序时最坏复杂度)
        mid = (low + high) // 2
        arr[low], arr[mid] = arr[mid], arr[low]
        pivot_pos = partition(arr, low, high)
        quick_sort(arr, low, pivot_pos - 1)
        quick_sort(arr, pivot_pos + 1, high)


from random import randint
from time import clock

l = []
for i in range(100000):
    l.append(randint(0, 100000))
t = clock()
quick_sort(l, 0, 100000 - 1)  # python
print(clock() - t)
print(l[:100])

l = []
for i in range(100000):
    l.append(randint(0, 100000))
t = clock()
l.sort()  # python-stable
print(clock() - t)
print(l[:100])

from sort import quick_sort

l = []
for i in range(100000):
    l.append(randint(0, 100000))
t = clock()
quick_sort(l, 0, 100000 - 1)  # cython
print(clock() - t)
print(l[:100])

运行代码

当前目录下命令行中输入

// linux
python setup.py build_ext --inplace 
// windows(若输入上一个报错)
python setup.py build_ext --inplace -DMS_WIN64

然后就可以运行main.py文件了

运行结果

运行结果

可见cython可加快数十倍,与C比较后也可发现,速度相差不大。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值