[Numpy-discussion] profiling line by line

Robert Kern robert.kern@gmail....
Fri Sep 26 19:36:47 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? 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?

There is now! Add

    import line_profiler
    ip.expose_magic('lprun', line_profiler.magic_lprun)

to your ipy_user_conf.py .

In [1]: %lprun?
Type:             Magic function
Base Class:       <type 'instancemethod'>
Namespace:        IPython internal
File:             /Users/rkern/hg/line_profiler/line_profiler.py
Definition:       %lprun(self, parameter_s='')
    Execute a statement under the line-by-line profiler from the
    line_profiler module.

      %lprun -f func1 -f func2 <statement>

    The given statement (which doesn't require quote marks) is run via the
    LineProfiler. Profiling is enabled for the functions specified by the -f
    options. The statistics will be shown side-by-side with the code through the
    pager once the statement has completed.


    -f <function>: LineProfiler only profiles functions and methods it is told
    to profile.  This option tells the profiler about these functions. Multiple
    -f options may be used. The argument may be any expression that gives
    a Python function or method object. However, one must be careful to avoid
    spaces that may confuse the option parser. Additionally, functions defined
    in the interpreter at the In[] prompt or via %run currently cannot be
    displayed.  Write these functions out to a separate file and import them.

    One or more -f options are required to get any useful results.

    -D <filename>: dump the raw statistics out to a marshal file on disk. The
    usual extension for this is ".lprof". These statistics may be viewed later
    by running line_profiler.py as a script.

    -T <filename>: dump the text-formatted statistics with the code side-by-side
    out to a text file.

    -r: return the LineProfiler object after it has completed profiling.

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

'tis gone now. The script and the requirement for argparse. Now you
just use line_profiler.py as a script.

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