[英]cpython vs cython vs numpy array performance
I am doing some performance test on a variant of the prime numbers generator from http://docs.cython.org/src/tutorial/numpy.html . 我正在对http://docs.cython.org/src/tutorial/numpy.html上的素数生成器的变体进行一些性能测试。 The below performance measures are with kmax=1000
以下性能测量值为kmax = 1000
Pure Python implementation, running in CPython: 0.15s 纯Python实现,在CPython中运行:0.15s
Pure Python implementation, running in Cython: 0.07s 纯Python实现,在Cython中运行:0.07s
def primes(kmax):
p = []
k = 0
n = 2
while k < kmax:
i = 0
while i < k and n % p[i] != 0:
i = i + 1
if i == k:
p.append(n)
k = k + 1
n = n + 1
return p
Pure Python+Numpy implementation, running in CPython: 1.25s 纯Python + Numpy实现,在CPython中运行:1.25s
import numpy
def primes(kmax):
p = numpy.empty(kmax, dtype=int)
k = 0
n = 2
while k < kmax:
i = 0
while i < k and n % p[i] != 0:
i = i + 1
if i == k:
p[k] = n
k = k + 1
n = n + 1
return p
Cython implementation using int*: 0.003s 使用int *:0.003s的Cython实现
from libc.stdlib cimport malloc, free
def primes(int kmax):
cdef int n, k, i
cdef int *p = <int *>malloc(kmax * sizeof(int))
result = []
k = 0
n = 2
while k < kmax:
i = 0
while i < k and n % p[i] != 0:
i = i + 1
if i == k:
p[k] = n
k = k + 1
result.append(n)
n = n + 1
free(p)
return result
The above performs great but looks horrible, as it holds two copies of the data... so I tried reimplementing it: 以上表现很好,但看起来很可怕,因为它拥有两份数据...所以我尝试重新实现它:
Cython + Numpy: 1.01s Cython + Numpy:1.01s
import numpy as np
cimport numpy as np
cimport cython
DTYPE = np.int
ctypedef np.int_t DTYPE_t
@cython.boundscheck(False)
def primes(DTYPE_t kmax):
cdef DTYPE_t n, k, i
cdef np.ndarray p = np.empty(kmax, dtype=DTYPE)
k = 0
n = 2
while k < kmax:
i = 0
while i < k and n % p[i] != 0:
i = i + 1
if i == k:
p[k] = n
k = k + 1
n = n + 1
return p
Questions: 问题:
how do I cast a numpy array to a int*? 如何将numpy数组转换为int *? The below doesn't work
以下不起作用
cdef numpy.nparray a = numpy.zeros(100, dtype=int) cdef int * p = <int *>a.data
cdef DTYPE_t [:] p_view = p
Using this instead of p in the calculations. 在计算中使用此代替p。 reduced the runtime from 580 ms down to 2.8 ms for me.
我将运行时间从580毫秒减少到2.8毫秒 。 About the exact same runtime as the implementation using *int.
关于与使用* int的实现完全相同的运行时。 And that's about the max you can expect from this.
这就是你可以期待的最大值。
DTYPE = np.int
ctypedef np.int_t DTYPE_t
@cython.boundscheck(False)
def primes(DTYPE_t kmax):
cdef DTYPE_t n, k, i
cdef np.ndarray p = np.empty(kmax, dtype=DTYPE)
cdef DTYPE_t [:] p_view = p
k = 0
n = 2
while k < kmax:
i = 0
while i < k and n % p_view[i] != 0:
i = i + 1
if i == k:
p_view[k] = n
k = k + 1
n = n + 1
return p
why is the numpy array so incredibly slower than a python list, when running on CPython?
为什么在CPython上运行时numpy数组比python列表慢得多?
Because you didn't fully type it. 因为你没有完全输入它。 Use
使用
cdef np.ndarray[dtype=np.int, ndim=1] p = np.empty(kmax, dtype=DTYPE)
how do I cast a numpy array to a int*?
如何将numpy数组转换为int *?
By using np.intc
as the dtype, not np.int
(which is a C long
). 通过使用
np.intc
作为np.int
,而不是np.int
(这是一个C long
)。 That's 那是
cdef np.ndarray[dtype=int, ndim=1] p = np.empty(kmax, dtype=np.intc)
(But really, use a memoryview, they're much cleaner and the Cython folks want to get rid of the NumPy array syntax in the long run.) (但实际上,使用memoryview,它们更干净,从长远来看,Cython人们想要摆脱NumPy数组语法。)
Best syntax I found so far: 到目前为止我找到的最佳语法:
import numpy
cimport numpy
cimport cython
@cython.boundscheck(False)
@cython.wraparound(False)
def primes(int kmax):
cdef int n, k, i
cdef numpy.ndarray[int] p = numpy.empty(kmax, dtype=numpy.int32)
k = 0
n = 2
while k < kmax:
i = 0
while i < k and n % p[i] != 0:
i = i + 1
if i == k:
p[k] = n
k = k + 1
n = n + 1
return p
Note where I used numpy.int32 instead of int. 注意我在哪里使用numpy.int32而不是int。 Anything on the left side of a cdef is a C type (thus int = int32 and float = float32), while anything on the RIGHT side of it (or outside of a cdef) is a python type (int = int64 and float = float64)
cdef左侧的任何内容都是C类型(因此int = int32和float = float32),而它右侧(或cdef外部)的任何内容都是python类型(int = int64和float = float64) )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.