Seeing the output of line tracing?

568 views
Skip to first unread message

CFK

unread,
Dec 4, 2014, 10:34:14 AM12/4/14
to cython...@googlegroups.com
Hi all, I'm trying to figure out how to use the linetrace directive correctly.  I've added the following to my setup.py file:

    directive_defaults['profile'] = True
    directive_defaults['linetrace'] = True
    directive_defaults['binding'] = True

and I've added "define_macros=[("CYTHON_TRACE", "1")]" as an argument to my Extension instance.  However, when I use python's pstats class, the output I see is the standard pstats output; I don't see the line-by-line output I was hoping to see.  How do I view the line-by-line output?

Thanks,
Cem Karan

Robert Bradshaw

unread,
Dec 10, 2014, 2:08:01 AM12/10/14
to cython...@googlegroups.com
pstats doesn't to line profiling, see
http://stackoverflow.com/questions/3927628/how-can-i-profile-python-code-line-by-line
which should work with the above directives.

Cem Karan

unread,
Dec 10, 2014, 5:36:36 AM12/10/14
to cython...@googlegroups.com, Robert Bradshaw
OK, thank you, I'll try it.

Thanks,
Cem Karan

Till Hoffmann

unread,
Dec 30, 2014, 11:24:29 AM12/30/14
to cython...@googlegroups.com
Hi, 

I am having similar troubles. Using the %lprun magic in iPython notebook I only get an empty output as shown below.

Timer unit: 1e-06 s

Total time: 0 s
File: /Users/tillhoffmann/temp/test.pyx
Function: cumulative_sum at line 1

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
     1                                           def cumulative_sum(int n):
     2                                               cdef int s = 0, i
     3                                               for i in range(n):
     4                                                   s += i
     5                                                   
     6                                               return s

The setup file I use to compile the cython code is

from distutils.core import setup
from Cython.Build import cythonize
from Cython.Compiler.Options import directive_defaults
from distutils.extension import Extension

directive_defaults['profile'] = True
directive_defaults['linetrace'] = True
directive_defaults['binding'] = True

sourcefiles = ['test.pyx']

extensions = [Extension("test", sourcefiles, define_macros=[('CYTHON_TRACE', '1')])]

setup(
    ext_modules = cythonize(extensions)
)

It would be great if someone could point me in the right direction!

Till 

Robert Bradshaw

unread,
Dec 31, 2014, 7:22:58 PM12/31/14
to cython...@googlegroups.com
Looks like Robert Kern's line profiler uses the __code__ attribute to
filter its line profiling. I fixed Cython and added an example/test:
https://github.com/cython/cython/blob/master/tests/run/line_profile_test.srctree

Unfortunately it only works for 'def' functions at the moment.
> --
>
> ---
> You received this message because you are subscribed to the Google Groups
> "cython-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to cython-users...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Cem Karan

unread,
Jan 1, 2015, 10:24:48 AM1/1/15
to cython...@googlegroups.com
I for one am grateful for anything I can get! Thanks for working on this!

Thanks,
Cem Karan

Till Hoffmann

unread,
Apr 20, 2016, 8:46:19 AM4/20/16
to cython-users
Hi,

I just wanted to follow up on this discussion. The line profiler works nicely for module-level `def` functions, thank you. Do you know whether it's possible to support instance-level `def` functions?

Till

Robert Bradshaw

unread,
Apr 21, 2016, 1:54:24 AM4/21/16
to cython...@googlegroups.com
On Wed, Apr 20, 2016 at 5:09 AM, Till Hoffmann <tillah...@gmail.com> wrote:
Hi,

I just wanted to follow up on this discussion. The line profiler works nicely for module-level `def` functions, thank you. Do you know whether it's possible to support instance-level `def` functions?

I don't think it'd be that hard. In fact, it might just work if you put "# cython: binding=True" at the top of your file. 

Till Hoffmann

unread,
Apr 21, 2016, 9:47:57 AM4/21/16
to cython...@googlegroups.com
Hi Robert,

I think the profiling itself already works but the line profiler isn't able to extract the code object from the instance method. I've put together a small illustration here: https://gist.github.com/tillahoffmann/832663099ec52edc1a92fa9154955843

Till

You received this message because you are subscribed to a topic in the Google Groups "cython-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/cython-users/FH3TYK8BkeA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to cython-users...@googlegroups.com.

tob...@turbostream-cfd.com

unread,
Aug 23, 2016, 2:09:39 AM8/23/16
to cython-users
Hi,

I was wondering if anyone has had any luck getting this to work for instance methods? I have the exact same problem as Till reported earlier.

Tobias

Stefan Behnel

unread,
Sep 1, 2016, 6:56:35 AM9/1/16
to cython...@googlegroups.com
Till Hoffmann schrieb am 20.04.2016 um 14:09:
> The line profiler works
> nicely for module-level `def` functions, thank you. Do you know whether
> it's possible to support instance-level `def` functions?

This is now implemented in latest master.

Have fun,

Stefan

Till Hoffmann

unread,
Sep 1, 2016, 6:58:04 AM9/1/16
to cython...@googlegroups.com
That's great, thank you Stefan!

Till
> --
>
> ---
> You received this message because you are subscribed to a topic in the Google Groups "cython-users" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/topic/cython-users/FH3TYK8BkeA/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to cython-users...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages