Recently, I needed to measure the execution time of a piece of python code. I used python’s timeit module to accomplish this task. The timeit module can also be accessed from the command line. I will also discuss about it succinctly in the end.

So, now let’s use the timeit module. We will use the timeit function from the timeit module to measure the execution time of a function named divide. The divide function is as below -

def divide(a, b):
return a / b

The signature of timeit.timeit function is as below -

timeit.timeit(stmt='pass', setup='pass', timer=<default timer>, number=1000000)

Here -

  1. stmt  - pass the statement(s) or the function or any block of code whose execution time needs to be measured.
  2. setup  - pass the setup code. This code runs only once. It is usually used to import modules.
  3. timer  - pass the timer that is used to measure the code. Default timer will be platform dependent.
  4. number  - the number of time the code will run. The default value is 1000000.

Remember, the timeit.timeit function will give us the sum of all the loop timings.

So, let’s use the timeit.timeit function to test our function’s execution time. We can do it in one line of code -

timeit.timeit("divide(10, 2.4)", "from __main__ import divide", number = 10)

The output will be -

1.4066696166992188e-05

It takes about 14 micro seconds for our function to run 10 times.

Now let’s change the values of number and see the timings -

  1. number  = 100, Timing = 6.794929504394531e-05
  2. number  = 1000, Timing = 0.0005660057067871094
  3. number = 10000, Timing = 0.005716800689697266
  4. number  = 100000, Timing = 0.04524493217468262

To run the timeit.timeit function multiple times on the same input parameters, we can use the timeit.repeat  function. This function returns a python list  which contains all the timings. The signature of timeit.repeat is as below -

timeit.repeat(stmt='pass', setup='pass',timer=<default timer>, repeat=3, number=1000000)

Here,

  1. stmt - pass the statement(s) or the function or any block of code whose execution time needs to be measured.
  2. setup - pass the setup code. This code runs only once. It is usually used to import modules.
  3. timer - pass the timer that is used to measure the code. Default timer will be platform dependent.
  4. number - the number of time the code will run. The default value is 1000000.
  5. repeat - the number of times the timeit.timeit function will be called. Default value of this is 3.

So, lets use the timeit.repeat function -

timeit.repeat("divide(10, 2.4)", "from __main__ import divide", number = 100000)

The output is a list as shown below -

[0.044139862060546875, 0.03439211845397949, 0.03469586372375488]

To get the best timings we can use python’s min  function as shown below.

min(timeit.repeat("divide(10, 2.4)", "from __main__ import divide", number = 100000, repeat = 100))

The output is -

0.034729957580566406

Using the Command Line

In case, you are interested in running the code from command line, use the following command -

python -m <name of module> -s <setup code> -n <number> -r <repeat> <statement or function name whose execution time needs to be measured>

There are also other options that you can pass. We can test the execution time of our divide  function that we created earlier using command line -

python -m timeit -s "from divide import divide" -n 100000 -r 100 "divide(10,2.4)"

The output will be -

100000 loops, best of 100: 0.324 usec per loop

We run the code in 1000000 loops for 100 times and get the best timing 0.324 usec per loop. Remember, here the output is usec per loop, using timeit.timeit function the output was the sum of all the loops.

I hope you liked my post, Thank You. Additionally, I recommend checking this Stack Overflow thread and this official documentation page.

Using IPython Notebook

In IPython Notebook, you can use the magic function %timeit to measure the execution time of your code.