Calculating code runtime in Python is easy with module timeit

Tram Ho

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:

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.

  1. 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:

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).

  1. 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.

  1. 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 :

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.

  1. timeit.default_timer() This function will define the module’s timer function, always time.perf_counter() .

III. Run the module from the CLI

The syntax for running the timeit function from CLI is as follows

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 default time.perf_counter()

  • -u or --unit=U

    The time unit you want to display. You can use nsec , usec , msec or sec

That is my summary of the timeit module. You can read more here . Thanks for reading and happy timing your function.

Share the news now

Source : Viblo