Possible Duplicate:
How to profile my code?
Whats are best practices and tools for profiling and performance testing python code?
Any quick wins here or recommendations.
CProfile seams popular and some great notes/answers below, both are very good answers/tutorials. Vote away and I’ll pick the top one in a day or two. Thanks @senderle and @campos.ddc
Once a problem area is found are there any idioms and/or tips for converting code to make it faster?
cProfileis the classic profiling tool. The basic way to use it is like so:Here I’ve called it on the test routine of a reference implementation of the mersenne twister that I wrote.
ncallsis the number of times a function was called.tottimeis the total time spent in a function, excluding the time spent in sub-function calls.percallistottime / ncalls.cumtimeis the time spent in the function including the time spent in sub-function calls. And the remaining data is as follows:filename:lineno(func_name).In most cases, look at
ncallsandtottimefirst. In the above data, you can see that the large majority of the time spent by this program happens inextract_number. Furthermore, we can see thatextract_numberis called many (1000014) times. So anything I can do to speed upextract_numberwill significantly speed up the execution of this test code. If it gains me a microsecond, then the gain will be multiplied by 1000014, resulting in a full second gain.Then I should work on
generate_numbers. Gains there won’t matter as much, but they may still be significant, and since that function burns another .7 seconds, there’s some benefit to be had.That should give you the general idea. Note, however, that the
tottimenumber can sometimes be deceptive, in cases of recursion, for example.