Nashville Python User Group

Timing and Profiling in IPython

Timing and profiling code is all sorts of useful, and it’s also just good ol’ fashioned fun (and sometimes surprising!).
In this post, I’ll introduce how to do the following through IPython magic functions:

%time & %timeit: See how long a script takes to run (one time, or averaged over a bunch of runs).

%prun: See how long it took each function in a script to run.

%lprun: See how long it took each line in a function to run.

%mprun & %memit: See how much memory a script uses (line-by-line, or averaged over a bunch of runs).

Installation & Setup

Please make sure you’re running IPython 0.11 or greater. This post was authored against Python 2.7 and
IPython 0.13.1.

$ pip install ipython
$ ipython --version
0.13.1

Most of the functionality we’ll work with is included in the standard library, but if you’re interested in line-by-line
or memory profiling, go ahead and run through this setup. First, install the following:

%time

%timeit

See how long a script takes to run averaged over multiple runs.

In [8]: %timeit 10*1000000
10000000 loops, best of 3: 38.2 ns per loop

%timeit will limit the number of runs depending on how long the script takes to execute. Keep in mind that the
timeit module in the standard library does not do this by default, so timing long running scripts that way
may leave you waiting forever.

The number of runs may be set with with -n 1000, for example, which will limit %timeit to a thousand iterations,
like this:

Memory Profiling

%mprun

See how much memory a script uses line by line. Let’s take a look at the same foo() function that we profiled with
%lprun - except this time we’re interested in incremental memory usage and not execution time.

What do you know?

Please leave other tips & tools in the comments below. I remember a while back seeing a video from someone who built a
profiling visualization in matplotlib, but I haven’t been able to dig it up. If you find it, please post it here, too!