要从具有附加值的列表中获取新列表,我曾经认为
list_b = [*list_a, value]
比list_b = list_a + [value]
性能更高,因为后者会生成中间[value]
。
然而,根据基准测试(在Python 3.12.3 / Windows 10中测试),似乎结果相反:
from timeit import timeit
import random
import matplotlib.pyplot as plt
num_data_points = 1000
step = 10
methods = [
# ordered from slowest to fastest to make the key easier to read
# "list_b = list_a.copy(); list_b += (value,)",
# "list_b = list_a.copy(); list_b.append(value)",
# "list_b = list(list_a); list_b.append(value)",
"list_b = [*list_a, value]",
"list_b = list_a + [value]",
]
x = list(range(0, num_data_points * step, step))
y = [[] for _ in methods]
for i in x:
list_a = list(range(i))
random.shuffle(list_a)
value = random.randint(0, num_data_points * step)
setup = f"list_a = {list_a}; value = {value}"
for method_index, method in enumerate(methods):
y[method_index].append(timeit(method, setup=setup, number=800))
print(i, "out of", num_data_points * step)
ax = plt.axes()
for method_index, method in enumerate(methods):
ax.plot(x, y[method_index], label=method)
ax.set(xlabel="size of the list", ylabel="time (s) (lower is better)")
ax.legend()
ax.figure.canvas.get_default_filetype = lambda: 'svg'
plt.show()
有人可以帮忙解释一下原因吗?
据我所知,差异非常小。我认为这是由于使用 *.我记得在另一个用例中,它在迭代列表时增加了一点开销。