Python built-in type performance analysis
Timeit Module
Class Timeit. Timer (stmt= "Pass", setup= ' Pass ', Time=<timer function>)
A timer is a class that measures the execution speed of a small piece of code
The stmt parameter is the code statement to be tested (statment)
The setup parameter is the setting that is required to run the code
The timer parameter is a timer function, independent of the platform.
Timeit. Timer.timeit (number=1000000)
The object method that tests the execution speed of the statement in the Timer class, the number parameter is the test book count when the code is measured, and the default is 1 million times. method returns the average time spent executing code, and the number of seconds for a float type.
def t1 ():
Li = []
For I in range (10000):
Li.append (i)
Def t2 ():
Li = []
For I in range (10000):
Li + = [i]
def t3 ():
Li = [i for I in range (10000)]
def t4 ():
Li = List (range (10000))
Def T5 ():
Li = []
For I in range (10000):
Li.extend ([i])
Timer1 = Timeit ("T1 ()", "from __main__ import T1", number=1000)
Print ("Append:", timer1)
Timer2 = Timeit ("T2 ()", "from __main__ import T2", number=1000)
Print ("+:", Timer2)
Timer3 = Timeit ("t3 ()", "from __main__ import T3", number=1000)
Print ("I for I in range (n):", Timer3)
Timer4 = Timeit ("t4 ()", "from __main__ import T4", number=1000)
Print ("List (range (n)):", Timer4)
Timer5 = Timeit ("T5 ()", "from __main__ import T5", number=1000)
Print ("Li.extend ([i]):", Timer5)
Data structure and algorithm (II.)