N-Body simulation Python 3.15
python3. Kernel time 6.42 s on the average machine, index 101× vs the fastest (96.5×–106×), 6.6 MB average memory, 6.6 MB peak. Cold first iteration 6.34 s, whole process 6.25 s.
How this number came about
- The task. Classic five-body gravitational simulation (Sun, Jupiter, Saturn, Uranus, Neptune) from the Computer Language Benchmarks Game, advanced 1,000,000 steps of 0.01 with a symplectic integrator. Each iteration starts from slightly different initial conditions (planet positions scaled by 1 + (iteration mod 997) * 1e-5; iteration 0 is the classic setup). The energy before and after, rounded to 9 decimals, is the result. Every iteration gets a new, never reused instance from the deterministic generator lbgen (streamed to the program on standard input). A separate check compares an order-sensitive hash of the complete result of 20 iterations with the generator's reference; this was the case in every session.
- The environment. A fresh GitHub-hosted VM per session runs the pinned Docker image
python@sha256:3d35e8d5e482ef4f6151426e59208ecde73b7a80e88b38c1b098f60662e5fb91containingPython 3.15.2. Hardware, kernel and glibc of every session are listed below. - The source. Exactly this code (hash
7a60721f58d489be, in git at the commit of each session) is the kernel: it only decodes the instance and solves it. It is built together with the language's one harness, which owns the clock, the loop and the output:
# N-body simulation, after the Computer Language Benchmarks Game.
import math
import struct
STEPS = 1000000
def setup(args):
global STEPS
if args:
STEPS = int(args[0])
def decode(data):
v = struct.unpack("<35d", data)
return [list(v[i * 7 : i * 7 + 7]) for i in range(5)] # x y z vx vy vz mass
def energy(b):
e = 0.0
for i in range(5):
bi = b[i]
e += 0.5 * bi[6] * (bi[3] * bi[3] + bi[4] * bi[4] + bi[5] * bi[5])
for j in range(i + 1, 5):
bj = b[j]
dx = bi[0] - bj[0]
dy = bi[1] - bj[1]
dz = bi[2] - bj[2]
e -= bi[6] * bj[6] / math.sqrt(dx * dx + dy * dy + dz * dz)
return e
def solve(b):
solar = 4 * math.pi * math.pi
px = py = pz = 0.0
for o in b:
px += o[3] * o[6]
py += o[4] * o[6]
pz += o[5] * o[6]
b[0][3] = -px / solar
b[0][4] = -py / solar
b[0][5] = -pz / solar
e0 = energy(b)
dt = 0.01
pairs = [(b[i], b[j]) for i in range(5) for j in range(i + 1, 5)]
sqrt = math.sqrt
for _ in range(STEPS):
for bi, bj in pairs:
dx = bi[0] - bj[0]
dy = bi[1] - bj[1]
dz = bi[2] - bj[2]
d2 = dx * dx + dy * dy + dz * dz
mag = dt / (d2 * sqrt(d2))
bm = bi[6] * mag
cm = bj[6] * mag
bi[3] -= dx * cm
bi[4] -= dy * cm
bi[5] -= dz * cm
bj[3] += dx * bm
bj[4] += dy * bm
bj[5] += dz * bm
for o in b:
o[0] += dt * o[3]
o[1] += dt * o[4]
o[2] += dt * o[5]
return e0, energy(b)
def emit(res):
return [math.floor(e * 1e9 + 0.5) for e in res]
- Build and run.
- run
python3 /repo/languages/python/harness/harness.py /repo/languages/python/impl/nbody/main.py 1000000- kernel loop
- The harness reads and decodes an instance (untimed), times only
solvewith the language's monotonic clock, and repeats until the summed kernel time reaches 10 s (at least 3 iterations, no other cap). The first iteration is the cold time; the warm time is the median of the iterations after the first 2 s of kernel time. Memory is read from the container's cgroup by a sampler outside the process. - end-to-end
hyperfineruns the same binary for exactly one iteration (process start to exit, with the instance read from a file) and records wall-clock time and peak resident memory; reported next to the kernel time.- binary size
- -
- Many sessions, many iterations. The benchmark was measured in 4 independent sessions between 2026-10-27 and 2026-10-27, on 2 CPU models. Within a session the execution order of all benchmarks was randomised. Each dot below is one warm kernel iteration (at most 100 evenly spaced ones per session are shown); the black bar is the session median; orange dots are flagged outliers (kept in the data).
Warm-up curve
Kernel time of every iteration in order, one line per session (log axis; orange dot: the cold first iteration). Just-in-time and garbage-collected runtimes need a while to settle; compiled languages are flat from the start.
| Session | CPU | Iterations | Cold | Warm median | End-to-end | Machine factor | Warm ÷ factor | Avg. / peak memory | Source |
|---|---|---|---|---|---|---|---|---|---|
| 2026-10-27 | AMD EPYC 7763 64-Core Processor (4 cores) | 3 | 6.86 s | 6.88 s | 6.67 s | 1.058× | 6.51 s | 6.6 MB / 6.6 MB | Actions run · raw data · commit |
| 2026-10-27 | AMD EPYC 9V74 80-Core Processor (4 cores) | 3 | 6.28 s | 6.47 s | 6.62 s | 1.079× | 6.00 s | 6.6 MB / 6.6 MB | Actions run · raw data · commit |
| 2026-10-27 | AMD EPYC 7763 64-Core Processor (4 cores) | 3 | 7.23 s | 7.25 s | 6.79 s | 1.076× | 6.74 s | 6.6 MB / 6.6 MB | Actions run · raw data · commit |
| 2026-10-27 | AMD EPYC 7763 64-Core Processor (4 cores) | 3 | 6.84 s | 6.91 s | 6.66 s | 1.068× | 6.47 s | 6.6 MB / 6.6 MB | Actions run · raw data · commit |
The table is computed from the raw values: download every iteration and repetition of this benchmark (CSV), or the complete session files linked above. Each “Warm median” is the median of the kernel iterations after the first 2 s; “÷ factor” divides it by the machine factor.
few-iterations: fewer than 5 iterations fit the budget
- Cancelling out the hardware. GitHub gives every session a different computer, and some are faster than others, so raw times from different sessions cannot be compared directly. To fix that, every session gets a machine factor: how many times longer programs take on that machine than on the average machine. A factor of 1.2 means “20% slower than average”. We find these factors by looking at benchmarks that ran in several sessions: if the same programs are consistently 20% slower in one session, that machine is 1.2× slower. The factors for this benchmark's sessions are 1.058×, 1.079×, 1.076×, 1.068× (click a date in the table above for the session's details). Technically: we fit
log(time) = benchmark + machineacross all 8 sessions at once; see the methodology. - Combining sessions. Sessions, not iterations, are the independent samples. Each session's warm median divided by its machine factor (6.51 s, 6.00 s, 6.74 s, 6.47 s) is averaged geometrically: 6.42 s on the average machine (kernel (warm)). The 95% interval 6.12 s–6.67 s comes from a cluster bootstrap that resamples sessions, then iterations within each.
- Index vs fastest. 101× = this time ÷ the time of the fastest entry on the same problem, c 14 gcc-O3 (63.3 ms); interval 96.5×–106×.
- How much was hardware? Raw session medians differ by a geometric factor of 1.048; after dividing out the machine factors they differ by 1.050. The overall model leaves 1.057× of unexplained noise.
- Memory. A sampler on the host reads the anonymous memory of the container's cgroup every few milliseconds. The average is weighted by time over the timed kernel regions only (instance generation and decoding excluded), then averaged over sessions: 6.6 MB; the highest value seen was 6.6 MB.
Reproduce it
git clone https://github.com/StijnRis/language-bench.git && cd language-bench
pnpm install
pnpm lb verify python --version 3.15 --variant cpython-default --problems nbodyNeeds Docker. The command builds the harness and the program in the pinned image and checks the result against the generator's reference; the full timed measurement runs in CI sessions. The parameters of the problem are 1000000.