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

python四种列表的插入方法及其效率

时间:2019-06-09 23:55:49      阅读:236      评论:0      收藏:0      [点我收藏+]

标签:pen   lis   def   插入   四种   imp   for   from   print   

coding:utf-8

from timeit import timeit

def count_append():
lists = []
for x in range(0, 100000):
lists.append(x)

cost_time = timeit(stmt=count_append, number=1)
print("append花费的时间是", cost_time)

def count_extend():
lists = []
for x in range(0, 100000):
list1 = [x]
lists.extend(list1)

cost_time = timeit(stmt=count_extend, number=1)
print("extend花费的时间为", cost_time)

def count_insert():
lists = []
for x in range(0, 100000):
lists.insert(x, x)

cost_time = timeit(stmt=count_insert, number=1)
print("insert花费的时间为", cost_time)

def count_plus():
lists = []
for x in range(0, 100000):
list1 = [x]
lists = lists+list1

cost_time = timeit(stmt=count_plus, number=1)
print("plus花费的时间为", cost_time)

append花费的时间是 0.008006025272567989

extend花费的时间为 0.010472806737249051

insert花费的时间为 0.012643240513279762

plus花费的时间为 13.00245008582025

由此可得append是耗时最短的

python四种列表的插入方法及其效率

标签:pen   lis   def   插入   四种   imp   for   from   print   

原文地址:https://www.cnblogs.com/dxxpython/p/10995399.html

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