简体   繁体   English

为整个程序启用Cython分析?

[英]Enable Cython profiling for whole program?

The Cython docs say "Profiling in Cython is controlled by a compiler directive. It can be set either for an entire file or on a per function basis via a Cython decorator." Cython文档说“Cython中的分析是由编译器指令控制的。它可以通过Cython装饰器为整个文件或每个函数设置。”

Is there any easy way to enable Cython profiling for an entire Python program? 有没有简单的方法为整个Python程序启用Cython分析? That is, is there a way for me to not have to go through and add # cython: profile=True to dozens of files each time I want to turn profiling on and off? 也就是说,每次我想要打开和关闭分析时,有没有办法让我不必经历# cython: profile=True到几十个文件?

I believe you can set the directives globally by passing an option on the command line to cython . 我相信你可以通过在命令行cython选项传递给cython来全局设置指令。 It is described in the "Compilation" section of the documentation under "How to set directives" ( http://docs.cython.org/src/reference/compilation.html#how-to-set-directives ). 它在“如何设置指令”( 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: 也可以使用-X开关在命令行上传递指令:

$ cython -X boundscheck=True ... $ cython -X boundscheck = True ...

Directives passed on the command line will override directives set in header comments. 在命令行上传递的指令将覆盖在头注释中设置的指令。

If you are compiling through distutils ( setup.py ) and using the cythonize function, it appears that you can add the option compiler_directives , a dictionary that maps directive names to the corresponding value. 如果您正在通过distutilssetup.py )进行编译并使用cythonize函数,则可以添加选项compiler_directives ,这是一个将指令名称映射到相应值的字典。 I have not found documentation for this feature, but it appears to be how the cython program invokes the cythonize function ( https://github.com/cython/cython/blob/master/Cython/Build/Cythonize.py#L83 ). 我还没有找到这个功能的文档,但它似乎是cython程序如何调用cythonize函数( https://github.com/cython/cython/blob/master/Cython/Build/Cythonize.py#L83 )。

For example 例如

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM