【发布时间】:2016-08-27 12:32:44
【问题描述】:
我正在努力使用 Cython 加速一些 Python/Numpy 代码,但对“本地”设置(如文档中定义的 here)编译器指令的影响有点不清楚。就我而言,我想使用:
@cython.wraparound (False) #turn off negative indexing
@cython.boundscheck(False) #turn off bounds-checking
我知道我可以在我的 setup.py 文件中全局定义它,但我正在为非 Cython 用户开发,并且希望这些指令在 .pyx 文件中是显而易见的。
如果我正在编写一个.pyx 文件,其中定义了多个函数,我只需要设置一次,还是只适用于定义的下一个函数?我问的原因是文档经常说“关闭此函数的boundscheck”之类的内容,这让我想知道它是否仅适用于定义的下一个函数。
也就是说,我需要这样做吗:
import numpy as np
cimport numpy as np
cimport cython
ctypedef np.float64_t DTYPE_FLOAT_t
@cython.wraparound (False) #turn off negative indexing
@cython.boundscheck(False) # turn off bounds-checking
def myfunc1(np.ndarray[DTYPE_FLOAT_t] a):
do things here
def myfunc2(np.ndarray[DTYPE_FLOAT_t] b):
do things here
或者我需要这样做:
import numpy as np
cimport numpy as np
cimport cython
ctypedef np.float64_t DTYPE_FLOAT_t
@cython.wraparound (False) #turn off negative indexing
@cython.boundscheck(False) # turn off bounds-checking
def myfunc1(np.ndarray[DTYPE_FLOAT_t] a):
do things here
@cython.wraparound (False) #turn off negative indexing
@cython.boundscheck(False) # turn off bounds-checking
def myfunc2(np.ndarray[DTYPE_FLOAT_t] b):
do things here
谢谢!
【问题讨论】: