def list_append():
l = []
for i in range(5000):
l.append(i)

def list_insert():
l = []
for i in range(5000):
l.insert(0, i)

import timeit

# 默认执行1万次
append_spent = timeit.timeit(
setup='from __main__ import list_append',
stmt='list_append()',
number=10000,
)
print('list_append:', append_spent)

insert_spent = timeit.timeit(
setup='from __main__ import list_insert',
stmt='list_insert()',
number=10000,
)
print('list_insert:', insert_spent)

-------------------------------------------

个性签名:代码过万,键盘敲烂!!!