Profiling Cython with line_operator package

133 views
Skip to first unread message

afdel...@berkeley.edu

unread,
Jul 4, 2018, 12:47:38 AM7/4/18
to cython-users
I am trying to profile a method within a class I created using the line_profiler package. I found an example similar to my situation and in the .pyx, called collatz.pyx, it has 

@cython.binding(True)
cdef class CClass:

cpdef cp_pymethod(self, r):
for i in range(10):
a = r + 1
z = self.c_pymethod(a) + self.cmethod(r)
return z * 2

cdef cmethod(self, s):
for i in range(10):
p = s + 3
return p * 5

In the script used for profiling, it accesses the `cp_pymethod`c as follows:

from collatz import CClass
obj = CClass()
func = obj.cp_pymethod
profile = line_profiler.LineProfiler(func)
profile.runcall(func, 19)
assert_stats(profile, func.__name__)

However, when I run the test script, the output which is supposed to be articulate the times of each line in a call to cp_pymethod is instead blank. The output is:

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
    72                                               cpdef cp_pymethod(self, r):

Has this issue in line_profiler been resolved yet? If not, are there any other ways to profile a cython method line-by-line? 
Thanks!

0phoff

unread,
Aug 17, 2018, 10:06:38 AM8/17/18
to cython-users
Well your cp_pymethod is a C function (with a small python wrapper around it), so the line_profiler cannot trace over the different executed lines as it does not get parsed by the python interpreter. 
The easiest way to compare your cython code is to just time the execution with eg time.process_time() from the time module, or by using the `cython -a` command and see which lines still interact with python and which are getting compiled to pure C.

After googling around for a bit, it seems you can still trace cython code by setting the `linetrace` option. (Some relevant links: cython docs  -  SO1  -  SO2)
I have never tried this myself so not sure how this will affect your performance (profilers tend to slow everything down, so I dont know whether you can still compare python traced code with cython traced code)
Reply all
Reply to author
Forward
0 new messages