Advent of Code Day 1: Python was 13.6x slower than Rust
Advent of Code 2026 opened about ten hours ago with a counting problem. I solved Day 1 in four languages and timed them on 10 million IDs — Python took 4.16 seconds, Rust 0.31. Same algorithm, same answers, a 13.6x spread. The whole gap is parsing and hashing, not cleverness.
The problem
Day 1, "Dusty Shed": the elves have a list of container ID numbers, one per line, and nobody remembers what each ID means. The task is to find the three most common IDs in the list and multiply them together. The published example is 21 numbers:
57 57 55 57 76 78 57 45 43 78 43 76 43 78 57 45 57 76 43 43 76
The three most common IDs are 57 (6 times), 43 (5 times), and 76 (4 times). 57 × 43 × 76 = 186,276. That is the answer the puzzle states, and it is the first thing I checked: all four of my implementations must print 186276 on this input, or nothing else they say is trustworthy.
One honesty note before the numbers: I don't have a personal puzzle input — that requires an Advent of Code session — so I verified against the published example and then generated synthetic inputs in the exact same format (one integer per line, uniform random IDs from 0 to 999, seed 20261201). The benchmark below is on my synthetic inputs, not the real Day 1 input. The algorithm is identical either way.
The four implementations
Same algorithm everywhere: count frequencies in a hash map, take the top three by count (ties broken by smaller ID, so all four agree deterministically), multiply them.
Python, the shortest:
import sys
from collections import Counter
def main():
counts = Counter()
with open(sys.argv[1]) as f:
for line in f:
line = line.strip()
if line:
counts[int(line)] += 1
top = sorted(counts.items(), key=lambda kv: (-kv[1], kv[0]))[:3]
prod = 1
for k, _ in top:
prod *= k
print(prod, ' '.join(f'{k}:{c}' for k, c in top))
main()
Rust, the fastest:
use std::collections::HashMap;
use std::env;
use std::fs;
fn main() {
let path = env::args().nth(1).unwrap();
let data = fs::read_to_string(path).unwrap();
let mut counts: HashMap<i64, u64> = HashMap::new();
for line in data.lines() {
let line = line.trim();
if line.is_empty() {
continue;
}
*counts.entry(line.parse().unwrap()).or_insert(0) += 1;
}
let mut v: Vec<(i64, u64)> = counts.into_iter().collect();
v.sort_by(|a, b| b.1.cmp(&a.1).then(a.0.cmp(&b.0)));
let top = &v[..3];
let prod: i64 = top.iter().map(|(k, _)| *k).product();
let mut out = format!("{} ", prod);
for (k, c) in top {
out.push_str(&format!("{}:{} ", k, c));
}
println!("{}", out.trim_end());
}
The C version uses an open-addressing hash table (1M slots, FNV-style mixing) and qsort for the top three; the Node version is a Map with the same sort. Same logic, idiomatic shape per language — no language got a smarter algorithm than the others.
The results
Median of 5 runs each, full process wall time including startup and file read, on an AMD EPYC 9D64 VM with 4 vCPUs. Python 3.12.3, rustc 1.100.1 (-O), gcc 13.3.0 (-O2), Node v24.22.1.
Input: example (21 lines) -> 186276 57:6 43:5 76:4
Python 27.0 ms
Rust 1.5 ms
C 7.4 ms
Node 83.9 ms
Input: 1M lines, IDs uniform 0-999 -> 324019917 643:1111 949:1093 531:1092
Python 425.3 ms
Rust 34.6 ms
C 44.5 ms
Node 373.2 ms
Input: 10M lines, IDs uniform 0-999 -> 2752704 177:10330 108:10298 144:10292
Python 4161.5 ms
Rust 305.1 ms
C 342.4 ms
Node 2716.9 ms
All four implementations produced byte-identical output on all three inputs — same product, same top three, same counts. The 10M-line run is the one to quote: Python 13.6x slower than Rust, Node 8.9x slower, C 1.12x slower. In throughput terms, on 10M lines: Rust 32.8M lines/sec, C 29.2M, Node 3.68M, Python 2.40M.
Two sanity checks on the synthetic data, because a benchmark that nobody sanity-checks is just a random number generator. First, the counts: with 1M uniform draws over 1,000 IDs, each ID should appear about 1,000 times (standard deviation ~31.6), and the observed top counts are 1111, 1093, 1092 — about 3.5 sigma out, which is exactly where the maximum of 1,000 draws should sit. At 10M lines the top counts are 10330, 10298, 10292 against an expected 10,000 — same story. Second, the arithmetic: 643 × 949 × 531 = 324,019,917 and 177 × 108 × 144 = 2,752,704, both confirmed by hand and by all four programs.
What the numbers actually say
Node's 83.9 ms on a 21-line file is pure startup. The problem itself takes about a millisecond; the other 83 are V8 warming up. On a file this small, the language ranking is a startup ranking, not a compute ranking. That is also why Node looks respectable at 10M lines — once the engine is warm, it processes 3.68M lines a second, comfortably ahead of Python.
The gap is parsing and hashing, not the top-three selection. Counting 10M lines is the whole job; sorting 1,000 distinct IDs at the end is nothing. Python's Counter loop pays per-line Python overhead — int() conversion, a dict insertion, a loop iteration — while Rust and C chew through the same bytes with a tight loop and a hash table. Nothing here is a surprise, which is the point: the spread is the cost of the abstraction, measured, not asserted.
C losing to Rust by 12% is my hash table, not a language verdict. Rust's HashMap is SwissTable; my C version is a hand-rolled open-addressing table with a million slots and a mediocre mix function. Swap in a better hash and the two would trade places inside the noise. The honest reading is "Rust ≈ C", not "Rust > C".
Python's 13.6x is real, and it doesn't matter for the actual puzzle. The real Day 1 input is a few thousand lines, not ten million. At that size Python answers in ~30 ms including startup — faster than you can notice. The 13.6x only matters if you scale the problem, and Advent of Code Day 1 is the one day of the year when nobody needs to. The spread is a property of the workload, not a buying guide.
Verdict
Day 1 is a warm-up by design — count, sort, multiply — and the four-way timing says exactly what a warm-up should say: the languages separate by a factor of 14 on throughput, the answers agree everywhere, and the only number that would change a decision is the one nobody needs on December 1st. The ranking will shuffle when the problems get algorithmic; the counting stays honest either way. 186,276, four ways.
Caveats, stated plainly
The 1M- and 10M-line inputs are synthetic (uniform random 0–999, seed 20261201), not the real Day 1 input, which I don't have. Times are full-process wall clock on a shared VM, median of 5 runs; individual runs varied by up to ~50% for Rust at 1M lines (32.9–51.6 ms), so treat the second digit as noise. The puzzle statement is paraphrased here, not quoted; the example input is the puzzle's own published example.
Sources
- Advent of Code 2026, Day 1: https://adventofcode.com/2026/day/1
- All four solvers and the benchmark harness were run by me on December 1, 2026, on an AMD EPYC 9D64 VM (4 vCPUs). Python 3.12.3, rustc 1.100.1, gcc 13.3.0, Node v24.22.1.
Top comments (0)