#Load Robert Kern's line profiler %load_ext line_profiler import line_profiler #Set compiler directives (cf. http://docs.cython.org/src/reference/compilation.html) from Cython.Compiler.Options import directive_defaults directive_defaults['linetrace'] = True directive_defaults['binding'] = True %%cython -a -f --compile-args=-DCYTHON_TRACE=1 #We need to define the macro CYTHON_TRACE=1 (cf. http://docs.cython.org/src/reference/compilation.html) def cumulative_sum(int n): cdef int s=0, i for i in range(n): s += i return s #Print profiling statistics using the `line_profiler` API profile = line_profiler.LineProfiler(cumulative_sum) profile.runcall(cumulative_sum, 100) profile.print_stats() #Print profiling statistics using the `lprun` magic %lprun -f cumulative_sum cumulative_sum(100)