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 -

The signature of timeit.timeit function is as below -

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 -

The output will be -

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 -

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 -

The output is a list as shown below -

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

The output is -

## Using the Command Line

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

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 -

The output will be -

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.