码迷,mamicode.com
首页 > 编程语言 > 详细

1-06python列表类型不同操作的时间效率

时间:2019-07-03 22:58:55      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:xtend   pytho   运行   append   div   from   end   时间   pen   

from timeit  import Timer

def t1():
    li = []
    for i in range(10000):
        li.append(i)

def t2():
    li = []
    for i in range(10000):
            li +=  [i]

def t3():
    li = [i for i in range(10000)]

def t4():
    li = list(range(10000))

def t5():
    li = []
    for i in range(10000):
        li.extend([i])
def t6():
    li=[]
    for i in range(10000):
        li.insert(0,i)

timer1 = Timer("t1()","from __main__ import t1")
print("append:", timer1.timeit(1000))

timer2 = Timer("t2()","from __main__ import t2")
print("+:", timer2.timeit(1000))

timer3 = Timer("t3()","from __main__ import t3")
print("[i for i in range]:", timer3.timeit(1000))

timer4= Timer("t4()","from __main__ import t4")
print("list(range()):", timer4.timeit(1000))

timer5= Timer("t5()","from __main__ import t5")
print("extend:", timer5.timeit(1000))

timer6= Timer("t6()","from __main__ import t6")
print("insert:", timer6.timeit(1000))

运行结果:

append: 1.253803191
+: 1.6207985829999998
[i for i in range]: 0.4072384659999999
list(range()): 0.22763614999999993
extend: 1.818385053
insert: 31.748338614999998

 

1-06python列表类型不同操作的时间效率

标签:xtend   pytho   运行   append   div   from   end   时间   pen   

原文地址:https://www.cnblogs.com/echo-kid-coding/p/11126659.html

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