Data structures and algorithms (the timeit code execution time measuring module)

Python inspection efficiency, the python when calling the function, the function to examine the efficiency of its internal algorithms, the paper will be used to test the module in python timeit short python code execution speed.

class timeit.Timer(stmt=‘pass’, setup=‘pass’, timer=timer function)

Timer是测量小段代码执行速度的类
stmt参数是要测试的代码语句(statment)
setup参数是运行代码时需要的设置
timer参数是一个定时器函数,与平台有关

timeit.Timer.timeit(number=1000000)

Timer类中测试语句执行速度的对象方法,number参数是测试代码时的测试次数,默认为1000000,方法返回执行代码的平均耗时,一个float类型的秒数。

Time efficiency of different operations type list python

python common form to generate a list of what there are four:

  1. Addition of two lists
  2. List Builder
  3. Iterable into a list
  4. Additional elements of the empty list

For more than four forms, self-define four functions to achieve

#对空列表进行元素追加
def test1():
    li = []
    for i in range(10000):
        li.append(i)
def test2():
    li = []
    for i in range(10000):
        li =li + [i]
def test3():
    li = [i for i in range(10000)]
def test4():
    li = list(range(10000))

def test5():
    li = []
    for i in range(10000):
        li.extend([i])    
from timeit import Timer

timer1 = Timer('test1()','from __main__ import test1')
print("append追加:",timer1.timeit(1000))

timer2 = Timer('test2()','from __main__ import test2')
print("求和:",timer2.timeit(1000))

timer3 = Timer('test3()','from __main__ import test3')
print("列表生成器:",timer3.timeit(1000))

timer4 = Timer('test4()','from __main__ import test4')
print("可迭代对象转化:",timer4.timeit(1000))

timer5 = Timer('test5()','from __main__ import test5')
print("extend:",timer5.timeit(1000))

result

append added: 0.6496986700076377
sum: 0.7470834940031637
list generator: 0.30637266401026864
iterables conversion: 0.14538886799709871
Extend: 0.9688844589982182

Additional elements (append and insert)

def t6():
    li = []
    for i in range(10000):
        li.append(i) #尾部添加
        
def t7():
    li = []
    for i in range(10000):
        li.insert(0,i)#头部添加 
timer6 = Timer('t6()','from __main__ import t6')
print("append:",timer6.timeit(1000))

timer7 = Timer('t7()','from __main__ import t7')
print("insert:",timer7.timeit(1000))

result

append: 0.6802220060053514
insert: 28.873230566998245

The results look much better than the tail to add efficiency to add the head, which is a list of data storage python decision, which will be answered during the subsequent write data structures

Similarly, pop delete elements way, to the efficiency of the team head deletion far lower than removing elements from the tail efficiency.

list the time complexity of built-in functions

Operation Big-O Efficiency
index x[] O (1)
index assignment O (1)
append() O (1)
pop() O (1)
pop(i) O (n)
insert O (n)
del operator O (n)
iteration O (n)
contains(in) O (n)
get slice[x:y] O(k)
del slice O (n)
set slice O(n+k)
reverse O (n)
concatename O(k)
sort O (nlogn)
multiply O (nk)

dict time complexity of built-in functions

Operation Big-O Efficiency
copy O (n)
get item O (1)
set item O (1)
delete item O (1)
iteration O (n)
contains(in) O (1)
Published 49 original articles · won praise 11 · views 10000 +

Guess you like

Origin blog.csdn.net/sun_xiao_kai/article/details/104117663