SoFunction
Updated on 2024-10-28

python using the timeit time module

1. (stmt=‘pass', setup=‘pass', timer=<default timer>, number=default_number)

  • The timeit() function has four arguments, each of which is a keyword argument with a default value.
  • stmt: pass in the code that needs to be tested for time, either directly into a code expression or a single variable, or into a function. When passing in a function, put parentheses after the function name to allow the function to execute, e.g. stmt = 'func()'.
  • setup: pass stmt's runtime environment, such as parameters and variables used in stmt, modules to be imported, etc., such as setup = 'from __main__ import func'. You can write a one-line statement or a multi-line statement, separated by semicolons when writing multi-line statements.
  • If stmt and the parameter setup parameter are not passed a value, then the point of the test is lost, so these two parameters are necessary.
  • timer: 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 operating environment (already defined in the source code), just keep the default.
  • number: the number of runs of the code to be tested, the default is 1000000 (one million) times, for time-consuming code, run too many times will take a lot of time, you can modify the number of runs.

2. (stmt="pass", setup="pass", timer=default_timer, repeat=default_repeat, number=default_number)

  • The repeat() function has five parameters, each of which is a keyword parameter with a default value. The parameters have the same meaning as timer()
  • repeat: indicates how many times the test should be repeated, which can be interpreted as repeating the timeit() function with the same arguments. The final result is returned as a list, repeat is 3 times by default.

3. class (stmt=‘pass', setup=‘pass', timer=<timer function>)

  • Class that calculates the execution speed of a small piece of code, the constructor takes the arguments stmt, setup, timer.
  • The default value of the first two parameters is pass, timer default will be automatically obtained according to the operating system of the current operating environment; the first two parameters can contain more than one statement, use a semicolon between multiple statements;or new lines to separate
import timeit
def t1():
 li = [i for i in range(100000)]
def t2():
 li = []
 for i in range(100000):
  li += [i]
def t3():
 li = []
 for i in range(100000):
  (i)
def t4():
 li = []
 list(range(100000))
def t5():
 li = []
 for i in range(100000):
  ([i])
def t6():
 li = []
 for i in range(100000):
  (0,i)

# Mode 1: ()
# list_t1 = ('t1()','from __main__ import t1',number=1)
# print("i for i in range(100000): %s" %(list_t1))

# Mode 2: ()
# list_t2 = ('t2()','from __main__ import t2',repeat=1,number=1)
# print("li += i: %s" %(str(list_t2)))

# Way 3:
# timer3 = ('t3()','from __main__ import t3')
# print("(i): %s" %((number=1)))

# Pass the string object directly
# list_l4 = ('li = [i for i in range(1000)]',number=1)
# print(str(list_l4))

# Pass in multiple function objects (; or blank line separations)
list_l5 = ('t4();t5();t6()','from __main__ import t4;from __main__ import t5;from __main__ import t6',repeat=1,number=1)
print(str(list_l5))

Above is the detailed content of python using timeit statistics runtime module, more information about python using timeit please pay attention to my other related articles!