[英]Improving speed with Cython when dealing with Numpy ndarray
I'm testing Cython performance when a numpy ndarray is changed based on whether the sum of the i-th and j-th indices are even or odd. 我正在根据第i个索引和第j个索引的总和是偶数还是奇数来更改numpy ndarray时的Cython性能。 Compared to Python, the Cython speed is merely 80% faster, which is somewhat of a mediocre gain in speed.
与Python相比,Cython速度仅快80%,这在速度上有些中等。 I've ran out of ideas at the moment.
目前,我的想法已用尽。 Any suggestions?
有什么建议么?
@Python: @蟒蛇:
def even_odd_function(matrix):
dim = matrix.shape[1]
for i in range(dim):
for j in range(dim):
if (i + j) % 2 == 0:
matrix[i, j] = matrix[i, j] ** 2
else:
matrix[i, j] = matrix[i, j] * -1
return matrix
@Cython: @Cython:
%%cython
import numpy as np
cimport numpy as np
cimport cython
DTYPE = np.int
ctypedef np.int DTYPE_t
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.nonecheck(False)
def even_odd_function7(np.ndarray matrix):
cdef int dim = matrix.shape[1]
cdef int i
cdef int j
for i in range(dim):
for j in range(dim):
if (i + j) % 2 == 0:
matrix[i, j] = matrix[i, j] * matrix[i, j]
else:
matrix[i, j] = matrix[i, j] * -1
return matrix
You need to annotate the type of your array for the major speedup. 您需要注释阵列的类型以加快速度。
import numpy as np
cimport numpy as np
cimport cython
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.nonecheck(False)
def even_odd_function8(np.ndarray[np.float64_t, ndim=2] matrix):
cdef int dim = matrix.shape[1]
cdef int i
cdef int j
for i in range(dim):
for j in range(dim):
if (i + j) % 2 == 0:
matrix[i, j] = matrix[i, j] * matrix[i, j]
else:
matrix[i, j] = matrix[i, j] * -1
return matrix
In [20]: arr = np.random.randn(1000, 1000)
In [21]: %timeit even_odd_function(arr)
1 loop, best of 3: 636 ms per loop
In [22]: %timeit even_odd_function7(arr)
1 loop, best of 3: 480 ms per loop
In [24]: %timeit even_odd_function8(arr)
1000 loops, best of 3: 1.61 ms per loop
Largely a stylistic thing, but I prefer the newer typed memoryview syntax, which will do the same thing. 在很大程度上是一种风格,但是我更喜欢较新的typed memoryview语法,它将执行相同的操作。
def even_odd_function(np.float64_t[:,:] matrix)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.