我有两个cython代码片段(我在这里使用ipython):
%%cython --cplus
import numpy as np
cimport numpy as np
cimport cython
from libcpp.vector cimport vector
def autoregr5(np.ndarray[np.float64_t,ndim=1] arr,np.float64_t alph):
cdef vector[float] vec;
vec.reserve(arr.size)
vec.push_back(arr[0])
for i from 1 <= i < (arr.size):
vec.push_back(vec.back() * alph + arr[i])
return vec
%%cython --cplus
import numpy as np
cimport numpy as np
cimport cython
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.overflowcheck(False)
@cython.initializedcheck(False)
def autoregr6(np.ndarray[np.float64_t,ndim=1] arr,np.float64_t alph):
cdef np.ndarray[np.float64_t,ndim=1] result = np.zeros(arr.size,dtype = np.float64)
result[0]=arr[0]
for i from 1 <= i < (arr.size):
result[i] = result[i-1] * alph + arr[i]
#arr[i]
return result
问题是autoregr6在1e7浮点数的np.array上运行需要2.2秒(与python相当),而对于autoregr5,我的机器只需要250毫秒(i7 4790,ubuntu 14,python) 3.4,cython 0.23)。 像这样的操作似乎特别耗时:
result[i] = result[i-1]
虽然这些:
result[i] = arr[i]
执行得非常快。你能告诉我如何加快这个速度吗?