此篇博客是此时列表中各种方法添加数据时的效率问题,如有错误之处,望大家指出!
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)