我有lst = [0] * 10**6
过这样的经历:
5.4 ± 0.4 ms bytearray(lst)
5.6 ± 0.4 ms bytes(bytearray(lst))
13.1 ± 0.7 ms bytes(lst)
Python:
3.13.0 (main, Nov 9 2024, 10:04:25) [GCC 14.2.1 20240910]
namespace(name='cpython', cache_tag='cpython-313', version=sys.version_info(major=3, minor=13, micro=0, releaselevel='final', serial=0), hexversion=51183856, _multiarch='x86_64-linux-gnu')
我本来以为bytes(lst)
和bytearray(lst)
会一样快,或者bytearray(lst)
更慢,因为它是更复杂的类型(具有更多功能,因为它是可变的)。但事实恰恰相反。甚至绕行也bytes(bytearray(lst))
比 快得多bytes(lst)
!为什么bytes(lst)
这么慢?
基准测试脚本(在线尝试!):
from timeit import timeit
from statistics import mean, stdev
import random
import sys
setup = 'lst = [0] * 10**6'
codes = [
'bytes(lst)',
'bytearray(lst)',
'bytes(bytearray(lst))'
]
times = {c: [] for c in codes}
def stats(c):
ts = [t * 1e3 for t in sorted(times[c])[:5]]
return f'{mean(ts):5.1f} ± {stdev(ts):3.1f} ms '
for _ in range(25):
random.shuffle(codes)
for c in codes:
t = timeit(c, setup, number=10) / 10
times[c].append(t)
for c in sorted(codes, key=stats):
print(stats(c), c)
print('\nPython:')
print(sys.version)
print(sys.implementation)
受到一个也发现更快的答案的启发。bytearray