1

I am trying to run a line profiler on a function contained in a .pyx and compiled via distutils.setup. The function and its calls can be profiled with cProfile; however, I am trying to find a bottleneck and line profiling would help.

I have tried to set up Cython to use the below in the header of my pyx.

# cython: linetrace=True 
# distutils: define_macros=CYTHON_TRACE_NOGIL=1

I have also tried passing disutils the compiler directives and defined macros.

compiler_directives={'linetrace': True, 'bindings': True},
define_macros=[('CYTHON_TRACE_NOGIL', '1')])

No matter what I do, the line profiler cannot see the function. I get the following error message:

UserWarning: Could not extract a code object for the object <built-in function x>
  # contains convenience functions, global variable definitions, and visualization functions

Running the command line functions "kernprof" has the same effect with a slightly different error message (but obviously related).

'builtin_function_or_method' object has no attribute '__code__'

I am able to line_profile non-cython functions in the .py file without issue.

Chris McL
  • 190
  • 10
  • Possible duplicate of [How to profile cython functions line-by-line](https://stackoverflow.com/questions/28301931/how-to-profile-cython-functions-line-by-line) – ead Sep 05 '19 at 05:06
  • 2
    It is `binding` not `bindings`. You should add `# cython: binding=True` to you pyx-file – ead Sep 05 '19 at 05:09
  • Putting the # cython: binding=True in the header file worked. I had come across that (and apparently tried it in the compiler directives - but can't spell). It is clearer in the .pyx header anyway. Unfortunately, Cython doesn't discuss the binding property on the profiling page discussion. Thanks for the help! – Chris McL Sep 05 '19 at 14:00

0 Answers0