from timeit import Timer
import time
# l = []
# k = []
# s_time = time.time()
# for i in range(1000):
# l.append(i)
# e_time = time.time()
# f_time = s_time-e_time
# print(f_time)
# s_time = time.time()
# for i in range(1000):
# k.insert(1,i)
# e_time = time.time()
# f_time = s_time-e_time
# print(f_time)
def arg(pre=‘hello‘):
def t(function):
def inner():
s_time = time.time()
time.sleep(2)
function()
e_time = time.time()
f_time = e_time - s_time
print(‘{} is cost {} second‘.format(pre,f_time))
return inner
return t
@arg(‘test1‘)
def test1():
li = []
for i in range(10000):
li.append(i)
@arg(‘test2‘)
def test2():
li = []
for i in range(10000):
li+=[i]
@arg(‘test3‘)
def test3():
li = [i for i in range(10000)]
@arg(‘test4‘)
def test4():
li = list(range(10000))
@arg(‘test5‘)
def test5():
li = []
for i in range(10000):
li.extend([i])
# timer1 = Timer(‘test1()‘,‘from __main__ import test1‘)
# # timer1.timeit(1000)
# print(‘l‘,timer1.timeit(1000))
# timer2 = Timer(‘test2()‘,‘from __main__ import test2‘)
# print(‘i‘,timer2.timeit(1000))
# timer3 = Timer(‘test3()‘,‘from __main__ import test3‘)
# print(‘s‘,timer3.timeit(10000))
# timer4 = Timer(‘test4()‘,‘from __main__ import test4‘)
# print(‘t‘,timer4.timeit(10000))
test1()
test2()
test3()
test4()
test5()