Performance analysis of a data structure (code operating efficiency)

Test Module operation time Code: timeit.Timer
- the timeit Module: This module may be used to test the speed of execution of code section python / duration.

    - Timer class: class is length / time timeit python module designed to measure code execution speed. Prototype: class timeit.Timer (stmt = 'pass

      - stmt argument: that the upcoming test code block statements.

      - setup: Run's code block when the desired setting.

    - timeit function: timeit.Timer.timeit (number = 100000), the function returns the average time statement execution block number of times.

 

 1 from timeit import Timer
 2 
 3 
 4 def func1():
 5     l = []
 6     for i in range(1000):
 7         l += [i]
 8 
 9 
10 def func2():
11     l = []
12     for i in range(1000):
13         l.append(i)
14 
15 
16 def func3():
17     l = [i for i inRange (1000 )]
 18 is  
. 19  
20 is  DEF Func4 ():
 21 is      L = List (Range (1000 ))
 22 is  
23 is  
24  IF  the __name__ == ' __main__ ' :
 25      T1 = the Timer ( " func1 () " , " from __main__ Import func1 " )   # instance of the test object 
26 is      Print (t1.timeit (1000))   # test number 
27      T2 = the Timer ( " func2 () " , " from __main__ Import func2 ")
28     print(t2.timeit(number=1000))
29     t3 = Timer("func3()", "from __main__ import func3")
30     print(t3.timeit(1000))
31     t4 = Timer("func4()", "from __main__ import func4")
32     print(t4.timeit(1000))

 

Guess you like

Origin www.cnblogs.com/open-yang/p/11366974.html