Profiling Python functions with IPython’s timeit

If you aren’t careful (and even sometime when you are) realtime games written in Python sometimes hit speed problems and require some profiling to bring them to a playable speed. Typically, I would use the Python standard library’s “profile” module to find “hot” functions which are stealing all the CPU cycles. Today I discovered another way.

Over at scienceoss.com theres a nice post about profiling functions using “timeit” within IPython. Essentially, in IPython, you can run:

timeit my_slightly_sluggish_function(x, y)
timeit maybe_a_faster_function(x, y)

and get average execution time values over many replicates of each function call, like:

100000 loops, best of 3: 8.9 µs per loop
100000 loops, best of 3: 4.9 µs per loop

Interesting how programming for games and programming for scientific number crunching often have the same requirements and boil down to using similar techniques.

No related posts.

This entry was posted on Monday, April 14th, 2008 at 5:28 am and is filed under code, python. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

One Response to “Profiling Python functions with IPython’s timeit”

  1. Python Week 4: Union lists « YouGov Programming Challenges Says:

    [...] 3: Time it with an example dataset. Hint: ipython has a very convenienttimeit command. [...]

Leave a Reply