码迷,mamicode.com
首页 > 其他好文 > 详细

Basic_practice

时间:2018-01-21 12:38:43      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:form   div   hello   class   sleep   app   def   body   end   

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()

Basic_practice

标签:form   div   hello   class   sleep   app   def   body   end   

原文地址:https://www.cnblogs.com/hyolyn/p/8323601.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!