小编Ric*_*ham的帖子

Cython sum v/s意味着内存跳跃

我一直在尝试使用Cython,我遇到了以下特殊情况:数组上的sum函数占用数组平均值的3倍.

这是我的三个功能

cpdef FLOAT_t cython_sum(cnp.ndarray[FLOAT_t, ndim=1] A):
   cdef double [:] x = A
   cdef double sum = 0
   cdef unsigned int N = A.shape[0]
   for i in xrange(N):
     sum += x[i]
   return sum

cpdef FLOAT_t cython_avg(cnp.ndarray[FLOAT_t, ndim=1] A):
   cdef double [:] x = A
   cdef double sum = 0
   cdef unsigned int N = A.shape[0]
   for i in xrange(N):
     sum += x[i]
   return sum/N


cpdef FLOAT_t cython_silly_avg(cnp.ndarray[FLOAT_t, ndim=1] A):
   cdef unsigned int N = A.shape[0]
   return cython_avg(A)*N
Run Code Online (Sandbox Code Playgroud)

以下是ipython中的运行时间

In [7]: …
Run Code Online (Sandbox Code Playgroud)

python numpy cython

20
推荐指数
1
解决办法
707
查看次数

标签 统计

cython ×1

numpy ×1

python ×1