Getting line-level timings using hotshot

Paul McGuire ptmcg at
Mon Dec 22 22:43:58 CET 2003

I've googled and scanned docs and tried and errored, but have not seemed to
find the right combination of steps to get line-level timing and profiling
using hotshot.

>From the docs, I *think* I'm supposed to:

1. Create a hotshot.Profile object, giving the name of the output stats
file, and setting the lineevents and linetimings arguments to 1.
2. Run the code to be timed using the Profile object's run() method.
3. Close the output file by calling the Profile object's close() method.

This should create an output stats file containing the lineevents, with
timing information.

Now, I need to create a hotshot.stats StatsLoader object, using

At this point, things get fuzzier for me.  The examples I find on the web
typically follow load() with a call to strip_dirs(), but without much
explanation why.

Then the crucial question - what are the args to pass to sort_stats()?  I
can enumerate the options, but I'm afraid none of them are self-evident.
Here is the table of options from the ActivePython online docs:

    'calls' = call count
    'cumulative' = cumulative time
    'file' = file name
    'module' = file name
    'pcalls' = primitive call count
    'line' = line number
    'name' = function name
    'nfl' = name/file/line
    'stdname' = standard name
    'time' = internal time

(This question is complicated by the ability to specify multiple options in
the sort_stats() call.  But no matter what combination I try, I still only
get function level time reporting.)

Lastly, print_stats(n) gets called, to list out the 'n' highest entries in
the sorted list.

Can anyone help me out?  I know which functions are most time consuming now,
but what lines in those functions?  What are the missing steps to get line
level timing for performance tuning?

-- Paul

More information about the Python-list mailing list