Profiler - counting individual lines' exec time

Skip Montanaro skip at mojam.com
Sat Aug 21 20:39:26 EDT 1999


    £ukasz> I have a loop in my program which loops hundreds of thousands
    £ukasz> times and I want to know which instructions in it take the
    £ukasz> most time to execute. Is that possible?

Time per line?  I don't know if there's a module that does that today.  As I 
recall, profile does per-function time measurement.  You might try my trace
module (check http://www.musi-cal.com/~skip/python/ for a pointer).  It
counts the number of times each line is executed.  Helpful, perhaps, but
still not quite what you want.  You could instrument it to collect timing
information as well, but that would make it even slower than it already is.
(Perhaps that's a good Saturday evening activity for me... hmmm... ;-)

Perhaps the best way to measure what you want in a fairly unobtrusive way is
to approximate it.  Set an alarm to ring periodically (every 0.05 or 0.1 s,
let's say).  When the alarm goes off, look at the program's current line
number and allocate the entire time slice to the current line.  Averaged
over a large number of samples, it should approximate the timing behavior of
the program as a whole.

You might be able to modify the python debugger or the profile module to do
this rather easily.

Skip Montanaro	| http://www.mojam.com/
skip at mojam.com  | http://www.musi-cal.com/~skip/
847-971-7098




More information about the Python-list mailing list