[IPython-user] how to use line_profiler, %lprun

Robert Kern robert.kern@gmail....
Mon Feb 9 17:42:00 CST 2009


On 2009-02-09 17:30, Gary Pajer wrote:
> I can't figure out how to use %lprun.
> kernprof from the os command line works fine.
> I have the two set-up lines in ipy_user_conf.py
>
> I have script, say:
>
> def foo(a,b):
>      a=a+1
>      b+=1
>      return a+b
>
> foo(1,2)
>
> which is saved as test.py
>
> I guessed
> %lprun -f foo test.py
> no, that's not it.
>
> the docstring has "<expression>" in it, not
> "<name-of-script-to-profile>", so maybe
> %lprun -f foo import test
> but that's not it either.
>
> I added the @profile decorator.  No, not yet
> (although
> %lprun test.py
> does work with the decorator)

I doubt it. %lprun doesn't take a file but an expression.

> What's the right thing to do?

Omit the foo(1,2) call at the end.

In [13]: !cat test.py
IPython system call: cat test.py
def foo(a,b):
      a=a+1
      b+=1
      return a+b


In [14]: import test

In [15]: %lprun -f test.foo test.foo(1,2)
Timer unit: 1e-06 s

File: test.py
Function: foo at line 1
Total time: 9e-06 s

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
      1                                           def foo(a,b):
      2         1            5      5.0     55.6       a=a+1
      3         1            2      2.0     22.2       b+=1
      4         1            2      2.0     22.2       return a+b


-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco



More information about the IPython-user mailing list