The timeit module in Python 3 can be used to test the running time of small pieces of code
There are two main functions to implement it: timeit and repeat. The code is as follows:
def timeit(stmt="pass", setup="pass", timer=default_timer,
number=default_number, globals=None):
"""Convenience function to create Timer object and call timeit method."""
return Timer(stmt, setup, timer, globals).timeit(number)
def repeat(stmt="pass", setup="pass", timer=default_timer,
repeat=default_repeat, number=default_number, globals=None):
"""Convenience function to create Timer object and call repeat method."""
return Timer(stmt, setup, timer, globals).repeat(repeat, number)
As you can see in the code above, both timeit and repeat are Mr. Timer objects, and then the timeit or repeat functions of the Timer objects are called.
When using timeit module, you can directly use timeit.timeit(), tiemit.repeat(), you can also use timeit.Timer() to generate a Timer object, then use TImer object with timeit() and repeat() functions, the latter is more flexible.
The parameters of the above two functions are as follows:
Stmt: The code used to pass in the time to be tested can accept either the expression of a string directly, a single variable, or a function. When passing in a function, declare the function in the current file, then execute the function at stmt ='func()', and then use setup ='from main import func'
setup: the running environment of stmt, such as parameters, variables used in stmt, modules to be imported, etc. You can write a single line statement or a multi-line statement. When you write a multi-line statement, you need to use semicolons; separate statements.
Number: The number of runs of the code to be tested is 100,000 by default. For time-consuming code, running too many times will be slow. At this time, it is recommended that you modify the number of runs.
Repat: repeat the test several times, each time the results constitute a list of returns, default 3 times.
First, use timeit.timeit(), tiemit.repeat():
import timeit
print(timeit.timeit(stmt= 'list(i**2 for i in normal_list)',setup = 'normal_list=range(10000)',number=10))
#0.3437936799875755
print(timeit.repeat(stmt= 'list(i**2 for i in normal_list)', setup='normal_list=range(10000)',repeat=2,number=10))
#[0.33649995761778984, 0.3394490767789293]
#setup For compound statements
print(timeit.timeit(stmt= 'list(i**2 for i in normal_list)',setup = 'a=10000;normal_list=range(a)',number=10))
#0.33272367424748817
print(timeit.repeat(stmt= 'list(i**2 for i in normal_list)', setup='a=10000;normal_list=range(a)',repeat=2,number=10))
#[0.3323106610316342, 0.3356380911962764]
def func():
normal_list=range(10000)
L = [i**2 for i in normal_list]
#stmt As a function
print(timeit.timeit("func()", setup="from __main__ import func",number=10))
#0.12436874684622312
print(timeit.repeat("func()", setup="from __main__ import func",repeat=2,number=10))
#[0.12142133435126468, 0.12079555675148601]
It's faster to use functions directly.
Second, Mr. Timer calls timeit(), repeat():
import timeit # Generating timer timer1 = timeit.Timer(stmt= 'list(i**2 for i in normal_list)',setup = 'normal_list=range(10000)') # number and repeat parameters are also passed when timeit and repeat are invoked print(timer1.timeit(number=10)) #0.34721554568091145 print(timer1.repeat(repeat=2,number=10)) #[0.3391925079630199, 0.34103400077255097] # setup is a compound statement timer1 = timeit.Timer(stmt= 'list(i**2 for i in normal_list)',setup = 'a=10000;normal_list=range(a)') print(timer1.timeit(number=10)) 0.34383463997592467 print(timer1.repeat(repeat=2,number=10)) #[0.34573984832288773, 0.34413273766891006] # stmt is a function def func(): normal_list=range(10000) L = [i**2 for i in normal_list] timer1 = timeit.Timer("func()", setup="from __main__ import func") print(timer1.timeit(number=10)) #0.1223264363160359 print(timer1.repeat(repeat=2,number=10)) #[0.12266321844246209, 0.1264150395975001]