我发现 Python 3.14 创建列表比创建元组快。在我的电脑上,元素达到大约 ~20 个时就开始出现差别。
这挺有意思。是怎么做到的?元组也能用同样的优化吗?
我发现 Python 3.14 创建列表比创建元组快。在我的电脑上,元素达到大约 ~20 个时就开始出现差别。
这挺有意思。是怎么做到的?元组也能用同样的优化吗?
I've found that constructing a list beats constructing a tuple on Python 3.14. For my computer, this begins at roughly ~20 elements.
That seems rather intriguing. What made this possible? Could tuples receive that optimization as well?
能再提供一些细节吗,包括实际的测试代码(创建列表和元组各有几种方式)、测量方法和得到的数字?以前的 Python 版本也有这个差别吗?
我自己也研究过类似的问题,比较了下面四种写法。
1. list(values)
2. [*values]
3. tuple(values)
4. (*values,)
我记得 2 比 1 快,可是 3 又比 4 快,让我觉得奇怪。已经忘了当时得出了什么结论……
Would you provide some more specifics, including your actual benchmark code (lists and tuples can each be constructed several ways), the measurement method and the figures you got? Were older Python releases different, too?
I've investigated a similar question myself, looking at these four alternatives:
1. list(values)
2. [*values]
3. tuple(values)
4. (*values,)
As I recoll, 2 beat 1, whereas 3 beat 4, which seemed strange. I've forgotten what conclusion I reached there…
我刚用 values = itertools.repeat(None, 2**20) 又测了一遍。
4.02 ± 0.04 ms list(values)
3.97 ± 0.03 ms [*values]
4.76 ± 0.08 ms tuple(values)
7.91 ± 0.12 ms (*values,)
Python: 3.13.0 (main, Nov 12 2025, 14:27:38) [GCC 15.2.1 20250820]
脚本
from timeit import timeit
from statistics import mean, stdev
import random
import sys
prelude = """
from itertools import repeat
values = repeat(None, 2**20)
"""
expressions = ['list(values)', '[*values]',
'tuple(values)', '(*values,)']
samples = {expression: [] for expression in expressions}
for trial in range(120):
for expression in random.sample(expressions, len(expressions)):
elapsed = timeit(expression, prelude, number=1)
samples[expression].append(elapsed)
for expression in expressions:
fastest = [elapsed * 1e3 for elapsed in sorted(samples[expression])[:6]]
print(f'{mean(fastest):5.2f} ± {stdev(fastest):3.2f} ms ', expression)
print('\nPython:', sys.version)
运行这个示例!
([*values] 比 list(values) 快,说的是较短的可迭代对象,因为解释器能把 [*values] 直接当作语法处理。)
总之,我隐约记得创建元组时针对较短的情况做了优化,因为这才是常见用法。要放很多对象时,通常会选列表,而不是往元组里塞。优化较小输入的情况,也可能让较大输入变慢。
I've just repeated the test using values = itertools.repeat(None, 2**20):
4.02 ± 0.04 ms list(values)
3.97 ± 0.03 ms [*values]
4.76 ± 0.08 ms tuple(values)
7.91 ± 0.12 ms (*values,)
Python: 3.13.0 (main, Nov 12 2025, 14:27:38) [GCC 15.2.1 20250820]
script
from timeit import timeit
from statistics import mean, stdev
import random
import sys
prelude = """
from itertools import repeat
values = repeat(None, 2**20)
"""
expressions = ['list(values)', '[*values]',
'tuple(values)', '(*values,)']
samples = {expression: [] for expression in expressions}
for trial in range(120):
for expression in random.sample(expressions, len(expressions)):
elapsed = timeit(expression, prelude, number=1)
samples[expression].append(elapsed)
for expression in expressions:
fastest = [elapsed * 1e3 for elapsed in sorted(samples[expression])[:6]]
print(f'{mean(fastest):5.2f} ± {stdev(fastest):3.2f} ms ', expression)
print('\nPython:', sys.version)
Run This Example!
(The advantage of [*values] over list(values) applied to small iterables, since the interpreter can process [*values] directly as syntax.)
Regardless, my hazy recollection is that constructing tuples favors small ones, since those are what people typically need. Generally you'd choose a list rather than fill a tuple with lots of objects. Improving the small-input case can also make larger inputs slower.
大家好, 我刚开始做 Rust 编译器开发,正在尝试实现编译器优化。能否推荐一下业界或社区最常用的基准测试集,用来评估 Rust 生成的程序执行效率,也就是应用的运行时性能,而不是编译速度? 我目前在用 rust-perf/runtime-bench…