Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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."

Is there any easy way to enable Cython profiling for an entire Python program? 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?

like image 858
Thomas Johnson Avatar asked Dec 09 '14 17:12

Thomas Johnson


1 Answers

I believe you can set the directives globally by passing an option on the command line to 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).

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.

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. 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).

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})
)
like image 165
Jon Lund Steffensen Avatar answered Oct 24 '22 19:10

Jon Lund Steffensen