我正在努力使用 Cython 加速一些 Python/Numpy 代码,并且对“本地”设置(如定义的here http://docs.cython.org/en/latest/src/reference//compilation.html在文档中)编译器指令。就我而言,我想使用:
@cython.wraparound (False) #turn off negative indexing
@cython.boundscheck(False) #turn off bounds-checking
我知道我可以在我的全局中定义它setup.py
文件,但我正在为非 Cython 用户开发,并且希望指令从.pyx
file.
如果我正在写一个.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
Thanks!
The 文档 http://cython.readthedocs.io/en/latest/src/reference/compilation.html#globally指出如果要全局设置编译器指令,则需要在文件顶部添加注释。例如。
#!python
#cython: language_level=3, boundscheck=False
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)