# -*- coding: utf-8 -*-
from timeit import Timer
def t1():
list_one = []
for i in range(1000):
list_one.append(i)
def t2():
list_two = []
for i in range(1000):
list_two = list_two + [i]
def t3():
list_three = [i for i in range(1000)]
def t4():
list_four = list(range(1000))
if __name__ == '__main__':
# 一下方法按顺序各执行10000次
timer1 = Timer('t1()', 'from __main__ import t1')
print('1', timer1.timeit(number=10000), 's')
timer2 = Timer('t2()', 'from __main__ import t2')
print('2', timer2.timeit(number=10000), 's')
timer3 = Timer('t3()', 'from __main__ import t3')
print('3', timer3.timeit(number=10000), 's')
timer4 = Timer('t4()', 'from __main__ import t4')
print('4', timer1.timeit(number=10000), 's')
运行结果如下: