# 测试场景

$S=\sum_{i,j}A_{i,j}$

// array_sum.c
double reduce_sum(int arr_len, double* arr){
double s=0.0;
int i;
for (i=0; i<arr_len; i++){
s = s + *arr;
arr++;
}
return s;
}


# test_pointer.pyx
import numpy as np
cimport numpy as np

cdef extern from "array_sum.c":
double reduce_sum(int arr_len, double* arr)

cpdef rsum(int arr_len, np.ndarray[np.float64_t, ndim=2, mode="c"] arr):
cdef:
double* arr_ptr = <double *>arr.data
double res = 0.0
res = reduce_sum(arr_len, arr_ptr)
return res


$cythonize -i test_pointer.pyx  编译完成后会在当前路径下生成*.c文件和*.so文件： $ ll | grep test_pointer
-rw-r--r-- 1 root root  374450 Jul 25 14:52 test_pointer.c
-rwxr-xr-x 1 root root  234848 Jul 25 14:52 test_pointer.cpython-37m-x86_64-linux-gnu.so*
-rw-r--r-- 1 root root     347 Jul 25 15:02 test_pointer.pyx


# 调用Cython函数

In [1]: import numpy as np

In [2]: from test_pointer import rsum

In [3]: num=10000

In [4]: x=np.random.random((num,num))

In [5]: x.shape
Out[5]: (10000, 10000)

In [6]: %timeit s=np.sum(x)
38.3 ms ± 254 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)

In [7]: %timeit rs=rsum(num*num,x)
51.7 ms ± 302 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)

In [15]: np.sum(x)
Out[15]: 50003980.32921535

In [17]: rsum(num*num, x)
Out[17]: 50003980.32921728


# 版权声明

posted @ 2024-07-25 15:14  DECHIN  阅读(181)  评论(0编辑  收藏  举报