Hello everyone, today I would like to introduce a module very useful in measuring the run time of small code snippets: timeit.
I. How to use
- The timeit module can be used via CLI:
Ex:
python3 -m timeit 'print ([n**2 for n in range(1000)])'
- Or can be called in Python code:
Ex:
1 2 3 4 5 6 7 | <span class="token operator">>></span> <span class="token operator">></span> <span class="token keyword">import</span> timeit <span class="token operator">>></span> <span class="token operator">></span> <span class="token keyword">def</span> <span class="token function">some_computation</span> <span class="token punctuation">(</span> <span class="token punctuation">)</span> <span class="token punctuation">:</span> <span class="token punctuation">.</span> <span class="token punctuation">.</span> <span class="token punctuation">.</span> arr <span class="token operator">=</span> <span class="token punctuation">[</span> n <span class="token operator">*</span> <span class="token number">2</span> <span class="token operator">+</span> <span class="token number">1</span> <span class="token keyword">for</span> n <span class="token keyword">in</span> <span class="token builtin">range</span> <span class="token punctuation">(</span> <span class="token number">100</span> <span class="token punctuation">)</span> <span class="token punctuation">]</span> <span class="token punctuation">.</span> <span class="token punctuation">.</span> <span class="token punctuation">.</span> <span class="token keyword">print</span> <span class="token punctuation">(</span> <span class="token builtin">len</span> <span class="token punctuation">(</span> arr <span class="token punctuation">)</span> <span class="token punctuation">)</span> <span class="token punctuation">.</span> <span class="token punctuation">.</span> <span class="token punctuation">.</span> <span class="token operator">>></span> <span class="token operator">></span> timeit <span class="token punctuation">.</span> timeit <span class="token punctuation">(</span> some_computation <span class="token punctuation">,</span> number <span class="token operator">=</span> <span class="token number">500</span> <span class="token punctuation">)</span> |
II. Functions are provided
As you can see, by calling timeit.timeit
, you can calculate the run time of a function. The timeit module provides 3 convenient functions and 1 public class Timer.
- Class
timeit.Timer(stmt='pass', setup='pass', timer=<timer function>, globals=None)
This is the skeleton of the module. Most of the other functions of this module are just a wrapper function for this class. The Timer class can take several parameters. Statement ( stmt
) – is the code or function that you want to execute, setup
– is the code that you want to run to prepare some things before timing. Note that these two functions can be a string separated by signs ;
.
Ex:
1 2 | <span class="token operator">>></span> <span class="token operator">></span> timeit <span class="token punctuation">.</span> timeit <span class="token punctuation">(</span> <span class="token string">"a = 0; a += sum((n for n in range(100)))"</span> <span class="token punctuation">,</span> number <span class="token operator">=</span> <span class="token number">5</span> <span class="token punctuation">)</span> |
timer
is the timer
function and is set by default to the <default timer>
of the module. Finally, globals
are the parameter used to specify the namespace in which you execute the function (the default is implemented in the namespace of timeit).
- Function
timeit.timeit(stmt='pass', setup='pass', timer=<default timer>, number=1000000, globals=None)
This function will create an instance of the Timer
class, and then pass in this class the parameters the class requires as above. As can be seen that there is a new parameter, number
, you can use this parameter to determine the number of times that you will run the function.
- Function
timeit.repeat(stmt='pass', setup='pass', timer=<default timer>, repeat=5, number=1000000, globals=None)
Similar to the above, this function will also create an instance of the Timer
class. with parameters passed. This time add 1 parameter, repeat
next to number
. You can see that the repeat
and number
parameters can be confusing. You can understand these two parameters as follows: repeat
is the number of times that you run both the timeit
function, and number
is the number of times you run the code you want to measure. For example :
1 2 3 | <span class="token operator">>></span> <span class="token operator">></span> timeit <span class="token punctuation">.</span> repeat <span class="token punctuation">(</span> <span class="token string">"a = 0; a += sum((n for n in range(100)))"</span> <span class="token punctuation">,</span> number <span class="token operator">=</span> <span class="token number">5</span> <span class="token punctuation">,</span> repeat <span class="token operator">=</span> <span class="token number">6</span> <span class="token punctuation">)</span> <span class="token punctuation">[</span> <span class="token number">4.750300013256492e-05</span> <span class="token punctuation">,</span> <span class="token number">3.776699986701715e-05</span> <span class="token punctuation">,</span> <span class="token number">8.781899987297948e-05</span> <span class="token punctuation">,</span> <span class="token number">7.135699979698984e-05</span> <span class="token punctuation">,</span> <span class="token number">3.9323000237345695e-05</span> <span class="token punctuation">,</span> <span class="token number">3.5535999813873786e-05</span> <span class="token punctuation">]</span> |
As can be seen above, the timeit
function is repeated 6 times and returns 6 different results, each time it runs, the timeit
function runs the code that I passed in 5 times.
timeit.default_timer()
This function will define the module’stimer
function, alwaystime.perf_counter()
.
III. Run the module from the CLI
The syntax for running the timeit function from CLI is as follows
1 2 | python <span class="token operator">-</span> m timeit <span class="token punctuation">[</span> <span class="token operator">-</span> n N <span class="token punctuation">]</span> <span class="token punctuation">[</span> <span class="token operator">-</span> r N <span class="token punctuation">]</span> <span class="token punctuation">[</span> <span class="token operator">-</span> u U <span class="token punctuation">]</span> <span class="token punctuation">[</span> <span class="token operator">-</span> s S <span class="token punctuation">]</span> <span class="token punctuation">[</span> <span class="token operator">-</span> h <span class="token punctuation">]</span> <span class="token punctuation">[</span> statement <span class="token punctuation">.</span> <span class="token punctuation">.</span> <span class="token punctuation">.</span> <span class="token punctuation">]</span> |
The parameters are in the following order:
-n
or--number=N
The number of times the timeit function will run your code
-r
or--repeat=N
The number of times you want the timeit function to run
-s
or--setup=S
This will be the code you want to run to set up what is needed for the code to be measured.
-p
or--process
Here’s how to use a timer other than
time.process_time()
instead of the defaulttime.perf_counter()
-u
or--unit=U
The time unit you want to display. You can use
nsec
,usec
,msec
orsec
That is my summary of the timeit module. You can read more here . Thanks for reading and happy timing your function.