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.

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

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>