抱歉,您的浏览器无法访问本站
本页面需要浏览器支持(启用)JavaScript
了解详情 >

生成一个列表的几种方式的性能对比

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# -*- coding: utf-8 -*-

from timeit import Timer
import matplotlib.pyplot as plt

# 列表常用操作性能测试

# 迭代 + '+'
def test1():
l = []
for i in range(1000):
l = l + [i]


# 迭代 + append
def test2():
l = []
for i in range(1000):
l.append(i)

# 列表生成式
def test3():
l = [i for i in range(1000)]

# list构造函数 + range
def test4():
l = list(range(1000))

t1 = Timer("test1()", "from __main__ import test1")
# print("concat %f seconds" % t1.timeit(number=1000))

t2 = Timer("test2()", "from __main__ import test2")
# print("concat %f seconds" % t2.timeit(number=1000))

t3 = Timer("test3()", "from __main__ import test3")
# print("concat %f seconds" % t3.timeit(number=1000))

t4 = Timer("test4()", "from __main__ import test4")
# print("concat %f seconds" % t4.timeit(number=1000))

result = [t1.timeit(1000), t2.timeit(1000), t3.timeit(1000), t4.timeit(1000)]
method = ["for+ '+'", "for + append", "list comprehension", "list + range"]

plt.bar(method, result, color='rgby')

# plt.legend('concat time')
# print(zip(method, result))

for x,y in zip(method, result):
plt.text(x, y, "%fs" % y)

plt.show()

Cost time

list和dict的检索效率对比

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# -*- coding: utf-8 -*-

import random
from timeit import Timer
import matplotlib.pyplot as plt

lst_result = []
d_result = []

for i in range(10000,1000001,20000):
t = Timer("random.randrange(%d) in x" % i, "from __main__ import random,x")

x = list(range(i))
lst_time = t.timeit(number=1000)

x = {j:None for j in range(i)}
d_time = t.timeit(number=1000)

lst_result.append(lst_time)
d_result.append(d_time)
print("%d,%10.3f,%10.3f" % (i, lst_time, d_time))

test = [i for i in range(10000,1000001,20000)]

plt.plot(test, lst_result, 'ro')
plt.plot(test, d_result, 'bo')

plt.legend(['List','Dictionary'])

plt.show()

result plot

del list[index]与del dict[key] 性能对比

average time complexity:$ O(n)\ \ vs\ \ O(1) $

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# -*- coding: utf-8 -*-

import random
from timeit import Timer
import matplotlib.pyplot as plt


size = 20000


l_result = []
d_result = []

for i in range(size):
test_list = [i for i in range(size)]
list_t = Timer("del test_list[%d]" % i,"from __main__ import test_list")
list_result = list_t.timeit(number=1)
l_result.append(list_result)

test_dict = {j:None for j in range(size)}
dict_t = Timer("del test_dict[%d]" % i,"from __main__ import test_dict")
dict_result = dict_t.timeit(number=1)
d_result.append(dict_result)

# print("%d,%f,%f" % (i, list_result, dict_result))

plt.plot(range(size), l_result)
plt.plot(range(size), d_result)

plt.legend(['del list[index]', 'del dict[key]'])

plt.show()

result

参考

评论