bpo-47009: Streamline list.append for the common case by sweeneyde · Pull Request #31864 · python/cpython (original) (raw)

Benchmarks are good:

from pyperf import Runner, perf_counter

def bench_listcomp(loops, length): src = list(map(float, range(length))) t0 = perf_counter() for i in range(loops): [x for x in src] return perf_counter() - t0

def bench_append(loops, length): src = list(map(float, range(length))) t0 = perf_counter() for i in range(loops): arr = [] for x in src: arr.append(x) return perf_counter() - t0

runner = Runner() for n in [100, 1_000, 10_000, 100_000]: runner.bench_time_func(f"listcomp {n}", bench_listcomp, n) runner.bench_time_func(f"append {n}", bench_append, n)

Results from GCC on WSL with --enable-optimizations --with-lto

Faster (8):
- listcomp 10000: 118 us +- 2 us -> 92.6 us +- 1.5 us: 1.28x faster
- listcomp 100000: 1.16 ms +- 0.02 ms -> 916 us +- 26 us: 1.27x faster
- listcomp 1000: 12.3 us +- 0.2 us -> 9.89 us +- 0.41 us: 1.25x faster
- listcomp 100: 1.59 us +- 0.03 us -> 1.32 us +- 0.05 us: 1.21x faster
- append 100000: 1.69 ms +- 0.05 ms -> 1.45 ms +- 0.04 ms: 1.17x faster
- append 10000: 168 us +- 4 us -> 145 us +- 3 us: 1.16x faster
- append 1000: 17.4 us +- 0.3 us -> 15.2 us +- 0.6 us: 1.14x faster
- append 100: 2.03 us +- 0.06 us -> 1.81 us +- 0.08 us: 1.12x faster

Geometric mean: 1.20x faster