by kgm · 12y ago · 0 comments
$ 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:
$ gprof2dot.py -f pstats foo.pstats | dot -Tpdf -o foo.pdf
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).The result ends up looking like this: https://f.cloud.github.com/assets/150329/1570121/5713f02c-50...
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.