pyutils / line_profiler

Line-by-line profiling for Python
Other
2.66k stars 119 forks source link

Profiling cython functions - line by line doesn't get printed #13

Open mathijsmeulendijks opened 4 years ago

mathijsmeulendijks commented 4 years ago

When profiling a Cython cpdef function, LineProfiler.print_stats() only shows timings for the function definition line instead of line-by-line.

Expected behavior

File: myfile.pyx
Function: myfunction at line 17
Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
    17                                           cpdef void myfunction():
    18  64190160  250004471.0      3.9     58.6          for item in list:
    19  64178954  171814851.0      2.7     40.3              some_function(item)
 etc.

Observed behavior

Total time: 42.6412 s
File: myfile.pyx
Function: myfunction at line 17
Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
    17                                           cpdef void myfunction():

Details

LineProfiler.show_func uses inspect.getblock to get the lines of the profiled function. This doesn't work for cpdef functions. Python 3.75 on Windows line_profiler 3.0.2

Workaround

In the .pyx file, add a def (no cdef or cpdef) function at the end of the file.

Possible solution

Use an alternative for inspect.getblock

jakirkham commented 4 years ago

Related SO question.

bergkvist commented 4 years ago

Workaround (as mentioned above):

cpdef my_cython_function():
    x = 0
    for i in range(100):
        x += i**2
    return x

def this_function_causes_line_profiler_to_print_all_the_way_down_here():
    pass