language-bench

N-Body simulation

Advance the Jovian planets 1,000,000 steps with a symplectic integrator.

cpu-boundsimdmath

What is measured

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.

Pseudocode

bodies = decode(instance)             # position, velocity, mass; untimed
start timer
offset sun velocity so total momentum = 0
e0 = energy(bodies)
repeat 1,000,000 times:
    for each pair (i, j) with i < j:
        d    = pos[i] - pos[j]
        mag  = 0.01 / |d|^3
        vel[i] -= d * mass[j] * mag
        vel[j] += d * mass[i] * mag
    for each body b: pos[b] += 0.01 * vel[b]
e1 = energy(bodies)
stop timer
hash round(e0 * 1e9), round(e1 * 1e9)

Reveals: Scalar floating-point throughput, loop optimisation and (where the compiler can) auto-vectorisation.

Caveat: Result depends on iteration count, precision and compiler math flags. We forbid fast-math and FMA contraction so every language must produce the identical 9-digit energies.

Results (newest release of each language)

Language / buildTime (× vs best)
C 16 · gcc -O3 -march=nativedefault64.1 ms (1.01×)
C 16 · gcc -O364.2 ms (1.01×)
C 16 · gcc -O268.9 ms (1.09×)
Go 1.27 · GOAMD64=v3 go build80.2 ms (1.27×)
Go 1.27 · go builddefault80.5 ms (1.27×)
Python 3.15 · python3default6.42 s (101×)

Click a language to see the source code, build flags, every session and how the number was computed.