Mailing List Archive

Profiler - counting individual lines' exec time
Hi, everyone, it's my first posting on c.l.py.

I'm having hard time working with the Python profiler (profile.py). I have a
loop in my program which loops hundreds of thousands times and I want to know
which instructions in it take the most time to execute. Is that possible?

I imagine it could be done by tagging specific lines which execution time
I want measured but how do I do it? Using time.time() seems too messy and
not precise enough to me.

Moreover, documentation (html) provided seems to be incompatible with
reality :-) The Stats class does not provide any of the methods mentioned.

--
£ukasz Kowalczyk
http://tempac.fuw.edu.pl/~lukow/
Profiler - counting individual lines' exec time [ In reply to ]
£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@mojam.com | http://www.musi-cal.com/~skip/
847-971-7098
Profiler - counting individual lines' exec time [ In reply to ]
[ Skip Montanaro ]
[...]

Thanks a lot!

--
£ukasz Kowalczyk
http://tempac.fuw.edu.pl/~lukow/