gpt4 book ai didi

python - 为整个程序启用 Cython 分析?

转载 作者:太空狗 更新时间:2023-10-30 01:01:19 25 4
gpt4 key购买 nike

Cython 文档说“Cython 中的分析是由编译器指令控制的。它可以为整个文件设置,也可以通过 Cython 装饰器在每个函数的基础上设置。”

是否有任何简单的方法可以为整个 Python 程序启用 Cython 分析?也就是说,有没有一种方法可以让我不必在每次要打开和关闭分析时都向数十个文件添加 # cython: profile=True

最佳答案

我相信您可以通过将命令行上的选项传递给 cython 来全局设置指令。它在文档的“如何设置指令”( http://docs.cython.org/src/reference/compilation.html#how-to-set-directives ) 下的“编译”部分进行了描述。

One can also pass a directive on the command line by using the -X switch:

$ cython -X boundscheck=True ...

Directives passed on the command line will override directives set in header comments.

如果您正在通过 distutils (setup.py) 进行编译并使用 cythonize 函数,您似乎可以添加选项 compiler_directives,一个将指令名称映射到相应值的字典。我没有找到此功能的文档,但它似乎是 cython 程序如何调用 cythonize 函数(https://github.com/cython/cython/blob/master/Cython/Build/Cythonize.py#L83)。

例如

from distutils.core import setup
from Cython.Build import cythonize

setup(
name = "My hello app",
ext_modules = cythonize("src/*.pyx",
compiler_directives={'profile': True})
)

关于python - 为整个程序启用 Cython 分析?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27384570/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com