[Numpy-discussion] profiling line by line

Robert Kern robert.kern@gmail....
Wed Sep 17 15:33:43 CDT 2008

On Wed, Sep 17, 2008 at 07:12, Arnar Flatberg <arnar.flatberg@gmail.com> wrote:
> On Wed, Sep 17, 2008 at 3:56 AM, Robert Kern <robert.kern@gmail.com> wrote:
>> So, I could use some comments on the workflow. Does this look sensible
>> to everyone? How else would you like to use it?
> Works for me. I would love to use it as a part of an ipython session.
> Initially, I sprinkled som $profile in
> a library of my own, then I thought I would use the -s (SETUP) option to
> initialize some data to pass into the $profiled functions. However, I have
> probably misunderstood the meaning of the -s option. Could you give an
> example of its use?

It's mostly for using cProfile on a whole script. Since line_profiler
is already only active for certain functions, it's probably not all
that useful. At the time I added the feature, I was trying to profile
(part of) a GUI program using wx and VTK, both of which take a long
time to load and taking up a good chunk of my profile time. If I had a
script, load_wx_vtk.py:

  import wx
  from enthought.tvtk import tvtk

Then I could just do:

  $ kernprof.py -s load_wx_vtk.py my_script.py

The script at the end still needs to be the main thing that executes.

> I ended in doing a %edit in ipython to write a small
> test script with testdata and library imports, used kernprof.py and
> view_line_prof.py from ipython and captured the output from view_line_prof
> into ipython. Pheew .... now, there's perhaps an easier way?

It should be straightforward to make an ipython %magic, yes. There are
some questions as to how that would work, though. Do you want the
top-level code in the file to be profiled? Or do you just want
functions defined inside to be profiled? Do you want to be able to
specify functions defined outside of the code to be profiled?

> BTW, in the requirements you may want to mention argparse
> (view_line_prof.py)

Oops! Yes.

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 Numpy-discussion mailing list