import timeit
import random
for i in range(100000, 1000001, 100000):
t = timeit.Timer("random.randrange(%d) in x" %
i, "from __main__ import random,x")
x = list(range(i))
lst_time = t.timeit(number=1000)
x = {j: None for j in range(i)}
dic_time = t.timeit(number=1000)
print("%d,%10.3f,%10.3f" % (i, lst_time, dic_time))
运行结果:
100000, 0.560, 0.001
200000, 1.132, 0.002
300000, 1.624, 0.001
400000, 2.241, 0.001
500000, 2.668, 0.001
600000, 3.242, 0.001
700000, 3.898, 0.001
800000, 4.204, 0.001
900000, 4.880, 0.001
1000000, 5.627, 0.001
-
字典的执行时间与规模无关
-
列表的执行时间随规模的增大呈线性上升