[IPython-user] how to use line_profiler, %lprun
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):
> return a+b
> 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
> %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 : !cat test.py
IPython system call: cat test.py
In : import test
In : %lprun -f test.foo test.foo(1,2)
Timer unit: 1e-06 s
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
"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