Skip to content →

Quick and Easy Benchmarking in Python

I’m a fan of iPython. I use it on the command line instead of the regular python interpreter. It comes with a bunch of builtin “magic functions”; one of which is timeit, that allows you to show how long it takes for a Python statement or expression to complete.

Here is an example of timing the creating of a 10,000 x 10,000 two-dimensional list:

$ ipython
In [1]: %timeit [ [0] * 10000 for _ in range(10000) ]
734 ms ± 5.94 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

iPython ran this statement 7 times and reported that it took an average of 734 milliseconds to create the list.

Published in Today I Learned