18. Performance Profiling Python Code

Performance profiling is supported by the Python profile and pstats modules.

To create a profile file named profile.tmp for invocation of a function main() you would include the following code in your application:

import profile
profile.run('import mymodule; mymodule.main()', 'profile.tmp')

This will accumulate profile data while running the function main() in module mymodule. Note that importing and fully specifying the module scope is important if you plan to run the profiler under the Wing debugger. The profiler makes assumptions about scope that are violated by the debugger so just specifying profile.run('main()', 'profile.tmp') will not work.

Subsequently, the pstats module is used to inspect the contents of the profiler's output file. For example, the following command would sort the file by cumulative time spent in each function, and then print out the top 10 compute-intensive calls:

import pstats
p = pstats.Stats('profile.tmp')
p.sort_stats('cumulative').print_stats(10)  

Detailed documentation for profiling is available in the Python Library Reference under The Python Profiler.

Profiling and Debugging at the Same Time

Warning

Profiling under the debugger may yield inaccurate results since the debugger adds overhead that isn't always uniform across your code base.

The Python profiler makes some assumptions about how it is started that conflict with the way the Wing debugger works. Because the debugger can start debugging on the fly in the context of already-running code, it confuses the profiler into using the wrong top-level scope for its activities.

This means that a file like the following will not work:

import profile

def main():
  a = 1

profile.run("main()", "profile_tmp")

The profiler will raise an AttributeError on main because it is looking for it in the top-level file, which is not your code when running under Wing.

The way to solve this problem is to explicitly import and run the function you wish to profile, as follows:

import profile

def main():
  a = 1

profile.run("import mymodule; mymodule.main()", "profile_tmp")

« 17. Debugging Code Running Under Py2exeTable of ContentsWing IDE Reference Manual »

Navigation

Sub-sections

Wing IDE Tutorial
How-Tos
Wing IDE Reference Manual
Python Documentation
Mailing Lists
Priority Support
Development Support Services


EuroPython 2006

Wingware Stuff

Main menu

Site menu

About Wingware | Contact Us | Site Map |

Search