# 列表和字典操作的时间复杂度

### 列表操作的时间测试

timeit 模块的使用

def test1():
li = [for i in range(10000)]

#1、生成一个定时器对象
time1 = timeit.Timer('test1()','from __main__ import test1')  #参数接受要测试的代码块，以及导入的接口

#2、配置测试次数
ret = time1 = time1.timeit(100)
print(ret)



import timeit

def test1():
li = []

for i in range(10000):
li.append(i)

def test2():
li = []
for i in range(10000):
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.insert(-1, i)

if __name__ == '__main__':
timer1 = timeit.Timer('test1()', 'from test100 import test1')
timer2 = timeit.Timer('test2()', 'from test100 import test2')
timer3 = timeit.Timer('test3()', 'from test100 import test3')
timer4 = timeit.Timer('test4()', 'from test100 import test4')
timer5 = timeit.Timer('test5()', 'from test100 import test5')
print('t1', timer1.timeit(100))
print('t2', timer2.timeit(100))
print('t3', timer3.timeit(100))
print('t4', timer4.timeit(100))
print('t5', timer5.timeit(100))


t1 0.07158424341100353
t2 0.07301271975410409
t3 0.03303415543256208
t4 0.020644924926769975
t5 0.13402191468167618


### 列表与字典内置方法的时间复杂度

posted @ 2019-01-21 12:54  Kingfan  阅读(1444)  评论(0编辑  收藏  举报