Practice of using Python timeit module

Time:2021-1-24

The timeit module in Python can be used to test the execution time of a piece of code, such as the execution time of a variable assignment statement, the running time of a function, etc.

Timeit module is a module in Python standard library, which can be imported directly without installation. When importing, you can directly import timeit. You can use the timeit() function, the repeat() function, and the timer class. When using from timeit import… Only timer classes (with global variables) can be imported__ all__ Restrictions).

The source code of timeit module is only more than 300 lines in total. The main thing is to implement the above two functions and one class. You can take a look at it for yourself.

Next, I start to use the timeit module to test the code execution time. I use the timeit module to compare the execution time of adding data from the head and from the tail of Python list (what to test depends on the requirements).

1、 Use the timeit() function to test the run time

1. Prepare to test the function

First write two functions, one is to add data from the head of the list, the other is to add data from the tail of the list.


# coding=utf-8
def insert_time_test():
  insert_list = list()
  for i in range(10):
    insert_list.insert(0, i)
def append_time_test():
  append_list = list()
  for i in range(10):
    append_list.append(i)
if __name__ == '__main__':
  import timeit

2. timeit(stmt=”pass”, setup=”pass”, timer=default_ timer, number=default_ Introduction to number function

The timeit() function has four parameters, each of which is a keyword parameter with a default value.

Stmt: pass in the code that needs test time. You can directly pass in the code expression or a single variable, or you can pass in the function. When passing in a function, add parentheses after the function name to let the function execute. For example, stmt =’func() ‘.

Setup: the running environment of stmt, such as the parameters and variables used in stmt and the modules to be imported, such as setup =’from__ main__ import func’。 You can write one line or multiple lines, separated by semicolons.

If the stmt and setup parameters do not pass values, the significance of testing will be lost, so these two parameters are necessary.

Timer: the timer parameter is the basic time unit of the current operating system. By default, it will be automatically obtained according to the operating system of the current running environment (defined in the source code), and the default can be maintained.

Number: the number of runs of the code to be tested. The default is 1000000 (one million) times. For time-consuming code, it will take a lot of time to run too many times. You can modify the number of runs yourself.

3. Test the running time of the function

Now use < timeit() to test the runtime of the above two functions.


  insert_time_timeit = timeit.timeit(stmt='insert_time_test()', 
                    setup='from __main__ import insert_time_test')
  print('insert_time_timeit: ', insert_time_timeit)
  append_time_timeit = timeit.timeit(stmt='append_time_test()', 
                    setup='from __main__ import append_time_test')
  print('append_time_timeit: ', append_time_timeit)

Results of operation:

(‘insert_time_timeit: ‘, 2.9112871)
(‘append_time_timeit: ‘, 1.8884124999999998)

As you can see, it takes longer to add data at the head of the list than at the tail of the list.

4. Run time of test code (expression)

Continue to test the running time of the above code by using ﹣ timeit(), except that this time the code is passed directly into the parameter, not into the function.


  insert_time_timeit = timeit.timeit(stmt='list(insert_list.insert(0, i) for i in init_list)',
                    setup='insert_list=list();init_list=range(10)',
                    number=100000)
  print('insert_time_timeit: ', insert_time_timeit)
  append_time_timeit = timeit.timeit(stmt='list(append_list.append(i) for i in init_list)',
                    setup='append_list=list();init_list=range(10)',
                    number=100000)
  print('append_time_timeit: ', append_time_timeit)

Because of the long time, the number is changed from a million times to 100000 times. The results are as follows


('insert_time_timeit: ', 330.46189400000003)
('append_time_timeit: ', 0.21436310000001413)

Relatively speaking, for the same operation, the running time of using function is much less than that of directly passing in code expression, especially for inserting data in the header.

2、 Using the repeat() function to test run time

1. repeat(stmt=”pass”, setup=”pass”, timer=default_ timer, repeat=default_ repeat, number=default_ Introduction to number function

The repeat() function has five parameters, each of which is a keyword parameter with a default value. Compared with the timeit() function, the timeit() function has some parameters, and the repeat() function has one more parameter.

Repeat: indicates that the test should be repeated several times, which can be understood as repeating the timeit() function with the same parameter. The final result is returned as a list, and repeat is 3 times by default.

2. Test the running time of the function

Now test the running time of the above two functions by using < repeat().


 insert_time_repeat = timeit.repeat(stmt='insert_time_test()',
                    setup='from __main__ import insert_time_test')
  print('insert_time_repeat: ', insert_time_repeat)
  append_time_repeat = timeit.repeat(stmt='append_time_test()',
                    setup='from __main__ import append_time_test')

  print('append_time_repeat: ', append_time_repeat)

The results are as follows


('insert_time_repeat: ', [2.7707739, 2.908885, 2.7164823999999994])
('append_time_repeat: ', [1.7458063, 1.777368000000001, 1.8675014999999995])

3. Run time of test code (expression)

Continue to use “repeat() to test the running time of the above code. Pass in the code directly. After changing the number to 100000 times, the time is still very long (more than 300 seconds), so continue to reduce the number to 10000 times.


  insert_time_repeat = timeit.repeat(stmt='list(insert_list.insert(0, i) for i in init_list)',
                    setup='insert_list=list();init_list=range(10)',
                    repeat=5,
                    number=10000)
  print('insert_time_repeat: ', insert_time_repeat)
  append_time_repeat = timeit.repeat(stmt='list(append_list.append(i) for i in init_list)',
                    setup='append_list=list();init_list=range(10)',
                    repeat=5,
                    number=10000)
  print('append_time_repeat: ', append_time_repeat)

The results are as follows


('insert_time_repeat: ', [2.591015, 2.5814996999999997, 2.5547322, 2.6153070000000005, 2.5496864000000006])
('append_time_repeat: ', [0.0181692999999985, 0.01746889999999901, 0.018901899999999472, 0.018737400000000903, 0.018211900000000725])

3、 Using the timer class to test run time

1. Introduction of timer class

The timeit() function and repeat() function are used above. In fact, in the timeit module, these two functions further encapsulate the timer class, and actually call the methods in the timer class.

In the timer class, we implement two methods, the timeit () method and the repeat () method. The above two functions call these two methods.

When using “from timeit import…”, you can only import the timer class, so you can directly use the timer class to test. You can call the method yourself, which is more flexible.

2. Add test list header

First, instantiate an object of timer class. When instantiating, stmt and setup parameters are passed in (the meaning of the parameters is the same as above), and timer parameter remains the default. Then, call the corresponding timeit() method or repeat() method through the instance object, pass in number in the timeit() method, and number and repeat in the repeat() method.

Test the run time of adding data from the header using the time it () method and the time repeat () method.


 timer_insert = timeit.Timer(stmt='insert_time_test()', setup='from __main__ import insert_time_test')
  insert_time_timeit = timer_insert.timeit(number=1000000)
  print('insert_time_timeit: ', insert_time_timeit)
  insert_time_repeat = timer_insert.repeat(number=1000000)
  print('insert_time_repeat: ', insert_time_repeat)

The results are as follows


('insert_time_timeit: ', 2.7732486)
('insert_time_repeat: ', [2.7367806999999997, 2.707402600000001, 2.7288245999999994])

3. Add at the end of the test list

Test the run time of adding data from the tail using the time it () method and the time repeat () method.


  timer_append = timeit.Timer(stmt='append_time_test()', setup='from __main__ import append_time_test')
  append_time_timeit = timer_append.timeit(number=1000000)
  print('append_time_timeit: ', append_time_timeit)
  append_time_repeat = timer_append.repeat(number=1000000)
  print('append_time_repeat: ', append_time_repeat)

The results are as follows


('append_time_timeit: ', 1.9966106000000001)
('append_time_repeat: ', [1.9523343999999998, 1.8373857999999998, 1.8695377000000004])

The timeit module is a relatively simple module, which is about the usage.

summary

The above is the use practice of Python timeit module introduced by Xiaobian. I hope it can help you. If you have any questions, please leave me a message and Xiaobian will reply you in time. Thank you very much for your support to developer!
If you think this article is helpful to you, please reprint, please indicate the source, thank you!