Python内置类型性能分析--list

一、timeit模块

timeit模块可以用来测试一小段Python代码的执行速度。

     class timeit.Timer(stmt='pass', setup='pass', timer=<timer function>) timeit.Timer.timeit(number=1000000)

Timer是测量小段代码执行速度的类。
stmt参数是要测试的代码语句(statment)。
setup参数是运行代码时需要的设置。
timer参数是一个定时器函数,与平台有关。
Timer类中测试语句执行速度的对象方法。number参数是测试代码时的测试次数,默认为1000000次。方法返回执行代码的平均耗时,一个float类型的秒数。

二、list的操作测试

from timeit import Timer

def test1():
   l = []
   for i in range(1000):
      l = l + [i]
def test2():
   l = []
   for i in range(1000):
      l.append(i)
def test3():
   l = [i for i in range(1000)]
def test4():
   l = list(range(1000))

t1 = Timer("test1()", "from __main__ import test1")
print("concat ",t1.timeit(number=1000), "seconds")
t2 = Timer("test2()", "from __main__ import test2")
print("append ",t2.timeit(number=1000), "seconds")
t3 = Timer("test3()", "from __main__ import test3")
print("comprehension ",t3.timeit(number=1000), "seconds")
t4 = Timer("test4()", "from __main__ import test4")
print("list range ",t4.timeit(number=1000), "seconds")

结果:

('concat ', 1.2071499824523926, 'seconds')
('append ', 0.06817913055419922, 'seconds')
('comprehension ', 0.031675100326538086, 'seconds')
('list range ', 0.010011911392211914, 'seconds')

pop操作测试

x = range(2000000)
pop_zero = Timer("x.pop(0)","from __main__ import x")
print("pop_zero ",pop_zero.timeit(number=1000), "seconds")
x = range(2000000)
pop_end = Timer("x.pop()","from __main__ import x")
print("pop_end ",pop_end.timeit(number=1000), "seconds")

结果:

('pop_zero ', 3.614252805709839, 'seconds')
('pop_end ', 8.20159912109375e-05, 'seconds')

从结果可以看出,pop最后一个元素的效率远远高于pop第一个元素

点赞