Random notes from mg

a blog by Marius Gedminas

Marius is a Python hacker. He works for Programmers of Vilnius, a small Python/Zope 3 startup. He has a personal home page at http://gedmin.as. His email is marius@gedmin.as. He does not like spam, but is not afraid of it.

Sat, 06 Mar 2010

You've got to love profiling

Yesterday I slashed 50% of run time from our applications functional test suite by modifying a single function. I had no idea that function was responsible for 50% of the run time until I started profiling.

Profiling a Python program is getting easier and easier:

$ python -m cProfile -o prof.data bin/test -f

runs our test runner (which is a Python script) under the profiler and stores the results in prof.data.

$ runsnake prof.data

launches the RunSnakeRun profile viewer, which displays the results visually:

RunSnakeRun square map display
The square map display of RunSnakeRun, with the 'render_restructured_text' function highlighted.

Who knew that ReStructuredText rendering could be such a time waster? A short caching decorator and the test suite is twice as fast. The whole exercise took me less than an hour. I should've done it sooner.

Other neat tools:

posted at 20:49 | tags: | permanent link to this entry | 6 comments
Excellent post. Saved for future reference, thanks for sharing :-)
posted by Francesco at Sat Mar 6 23:41:26 2010
Oh man, I could have told you that restructuredtext rendering is really really slow :/
posted by Michael Hudson-Doyle at Mon Mar 8 03:35:55 2010
What about if i want to profile a gui app written in python? I do now want to profile the long start-up process, because i know that lots of libraries are being loaded.

Instead, i want to profile a specific function. Say, to profile what is happening inside a search function: from the point the user press "search" button till the app presents results in UI?

How would you do this?
posted by Darius Damalakas at Tue Mar 9 11:29:03 2010
Darius: profilehooks!

Find that function, slap the decorator:

  from profilehooks import profile
  @profile(filename='myfunc.prof', immediate=True)
  def myfunc(...):

run your app, push the button, a file named myfunc.prof will appear (and a summary will be printed to stdout).  Then use RunSnakeRun or any other tool of your choice.
posted by Marius Gedminas at Tue Mar 9 14:36:30 2010
Thanks, that looks like a reasonable solution.

Of course, i take for granted that it is possible to point more than one decorator to use the same file.
posted by Darius Damalakas at Mon Mar 15 13:32:00 2010
If you do that, one of them will overwrite data from the other one.

If you desire something else, well, patches welcome ;)
posted by Marius Gedminas at Mon Mar 15 13:49:39 2010

Name (required)


E-mail (will not be shown)


URL


Comment (some HTML allowed)