Luke Lee

Software Engineer

Web + Desktop + Science

Fork me on Github

Profiling Python code with cProfile and pstats

I've been trying to profile some pytables code lately and been doing a lot of reading on cProfile and pstats.

In short, do this to see how it works:

python -m cProfile -o stats <my_awesome_code>.py

python -m pstats stats

The pstats module even provides a command line interface to sort and list your profiling results! The downside is this seems to be relatively undocumented except for the in-command documentation (type 'help' at the prompt).

I found this article pretty useful.

It's pretty great that Python comes bundled with all of this be default, but be careful on Ubuntu apparently (from article):

Although the cProfile and pstats modules are listed on the Python Standard Library page, they were not installed by default on my setup (Ubuntu). A simple install of the python-profiler package did the trick.

Published: 05-18-2012 19:33:00