Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot use line_profiler with Cython

Based on the answer to this question I was trying to use the line_profiler with a cythonized function.

On the abovementioned question, the accepted answer gives us an example on how to use it with jupyter notebook.

However, when I try to build the pyx file using disutils it doesn't work.

We I plainly try to run the script using

kernprof -l -v script.py

It only returns the the Timer unit elapsed time.

If I try to decorate the function on the cython file using @profile, it doesn't compile returning:

undeclared name not builtin: profile

Any ideas ?

like image 621
Pedro Braz Avatar asked Aug 28 '17 19:08

Pedro Braz


1 Answers

The profile decorator is injected into the globals namespace by kernprof and is thus not available at compile time. However, you can apply the profile decorator to a function even after it has been defined. For example, in your script.py you could write the following.

from cython_module import function_to_be_profiled
# Apply the `profile` decorator
function_to_be_profiled = profile(function_to_be_profiled)

# Use function_to_be_profiled as intended

The third line of the snippet will fail if you run the script using standard python, i.e. python script.py, because the profile decorator is not defined. But it should behave as intended if you run it using kernprof.

like image 85
Till Hoffmann Avatar answered Nov 01 '22 07:11

Till Hoffmann