本地设置Cython编译器指令会影响一个或所有函数吗?

c14*_*369 3 python numpy cython

我正在加快与用Cython一些Python/numpy的代码,并在我的"本地"的设置(如定义的效果有点不清楚这里的文档)编译器指令.在我的情况下,我想使用:

@cython.wraparound (False) #turn off negative indexing
@cython.boundscheck(False) #turn off bounds-checking
Run Code Online (Sandbox Code Playgroud)

我知道我可以在我的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
Run Code Online (Sandbox Code Playgroud)

或者我需要这样做:

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
Run Code Online (Sandbox Code Playgroud)

谢谢!

Dun*_*nes 5

文档指出,如果要全局设置编译器指令,则需要在文件顶部添加注释.例如.

#!python
#cython: language_level=3, boundscheck=False
Run Code Online (Sandbox Code Playgroud)