Sometimes you need to get down to the core and make your code faster. One technique to speed up things is performance analysis through profiling.
Python 2.5 has revamped its support for profiling with the addition of cProfile which is (the name says all) a C-based version of the profile module. It’s definitely better than hotshot (which has been somewhat fixed in the latest version).
Django has a custom handler which enables mod_python powered apps to be profiled (with hotshot) without monkey patching the codebase.
What about internal webserver anyway?
There’s a wiki page about that. You simply have to patch django/core/management.py with this patch and Django will generate a handful of .prof files in /tmp or whatever each time you invoke the webserver. Then you have to read the stats with hotshot.stats.load or you can use gather_profile_stats.py and collect them all in aggregated profile files.
Sadly I didn’t get cProfile to work with Django because it lacks of a method returning the result of the profiled callable like the hotshot’s runcall but I didn’t spent much time on that so I guess the hack is just around the corner.
That’s it.
HTH

