python 列表三中元素添加方式快慢比较(append 加法+ insert)
import timet1 = time.time()s = []for i in range(10000):s = s+[3]print(time.time()-t1)t2 = time.time()k = []for i in range(10000):k.append(3)print(time.time()-t2)t3 = time.time()g = []for i in range(10
·
import time
t1 = time.time()
s = []
for i in range(10000):
s = s+[3]
print(time.time()-t1)
t2 = time.time()
k = []
for i in range(10000):
k.append(3)
print(time.time()-t2)
t3 = time.time()
g = []
for i in range(10000):
g.insert(-1, 3)
print(time.time()-t3)
输出
0.16560864448547363
0.0009524822235107422
0.002002716064453125
可见append 方法速度最快,而加法远远慢于append和insert
更多推荐
已为社区贡献3条内容
所有评论(0)