Learning Python the quick way

Tim Chase python.list at tim.thechases.com
Sat Apr 25 09:27:20 EDT 2009


Esmail wrote:
> Tim Chase wrote:
>> However the first rule:  profile first!
> 
> Do you have a favorite profiling tool? What should someone new
> to Python (but not programming) use?

I personally use the cheapo method of dropping in a few "print" 
statements to dump where I currently am in the app, and then see 
where it hangs.  "Hmm, it seems to take a long time to get from 
step J to step K, I better zoom in on that block of code"  I 
usually have a good intuition where to go digging first (such as 
multi-nested loops, network communications, file I/O, or database 
queries) which is fairly close most of the time.

If possible, I'll occasionally comment out the code I suspect (or 
perform a null-op instead of actually executing the block) to 
confirm that this block is the problem.  However, sometimes I 
can't readily do that because it sets up data structures that are 
needed elsewhere.

There are several profiling tools for Python, including the 
cProfile module[1] but I've never gotten to the point where I've 
needed that level of detail.

-tkc

[1] http://docs.python.org/library/profile.html

PS:  this is a trade-off between "what I've always done" and 
"darn, the stdlib provides yet one more battery I could be using 
instead of rolling my own".  I finally switched to using the csv, 
ConfigParser, pdb, and optparse modules because they were faster 
routes to better solutions than my own hacks.  However I still 
tend to just use print statements for profiling & logging instead 
of the logging and cProfile modules.  I guess I just haven't 
experienced enough pain in my own code to switch yet :)





More information about the Python-list mailing list