The built-in profiler can be told to dump the results of the profile to a "pstats" file. A simple way to do this is to run your code under the module itself, as in:
$ python -mcProfile -o foo.pstats foo.py
This will run your script (foo.py) inside of the profiler and save the profile to the file foo.pstats once the script is done.
Once you have a pstats file, you can do some interesting things with it. You can load it into an interactive pstats session with this:
$ python -mpstats foo.pstats
This will allow you to sort and display the profile data in a number of ways.
You can also use the third-party gprof2dot script[1] to turn the pstats file into a GraphViz dot file, and thence into a visualization of your code's performance. For example:
That will turn your foo.pstats file into a PDF. I like the PDF format here because it is vectorized, renders quickly (more quickly than SVG, at least), and you can search for specific text in it (which is useful if you have a large profile and want to find a specific function).
A couple years ago at a company hackathon, I took this approach, and then just baked these same tools into the app directly.
Django middleware to turn on/off and collect & store the profile data, and then a developer interface to read the stored profiles and render in SVG using pydot.
Once you have a pstats file, you can do some interesting things with it. You can load it into an interactive pstats session with this:
This will allow you to sort and display the profile data in a number of ways.You can also use the third-party gprof2dot script[1] to turn the pstats file into a GraphViz dot file, and thence into a visualization of your code's performance. For example:
That will turn your foo.pstats file into a PDF. I like the PDF format here because it is vectorized, renders quickly (more quickly than SVG, at least), and you can search for specific text in it (which is useful if you have a large profile and want to find a specific function).[1] https://code.google.com/p/jrfonseca/wiki/Gprof2Dot