language-bench

N-Body simulation › C

N-Body simulation C 14

gcc -O3 -march=native. Kernel time 63.4 ms on the average machine, index 1.00× vs the fastest (0.98×–1.02×), 0.4 MB average memory, 0.5 MB peak. Cold first iteration 63.5 ms, whole process 64.1 ms.

How this number came about

  1. 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.
  2. The environment. A fresh GitHub-hosted VM per session runs the pinned Docker image gcc@sha256:435c3cbc2ad1c25fbc1684c367808ff0ca2035a1bed56734a40165ab5cd7e3bd containing gcc (GCC) 14.4.0. Hardware, kernel and glibc of every session are listed below.
  3. The source. Exactly this code (hash 1b84ad87ec8bb134, 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. */
#include <math.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>

static const double SOLAR_MASS = 4 * 3.141592653589793 * 3.141592653589793;
static long steps;
static double body[5][7]; /* x y z vx vy vz mass */
static double energies[2];

static double energy(void) {
  double e = 0.0;
  for (int i = 0; i < 5; i++) {
    e += 0.5 * body[i][6] * (body[i][3] * body[i][3] + body[i][4] * body[i][4] + body[i][5] * body[i][5]);
    for (int j = i + 1; j < 5; j++) {
      double dx = body[i][0] - body[j][0], dy = body[i][1] - body[j][1], dz = body[i][2] - body[j][2];
      e -= body[i][6] * body[j][6] / sqrt(dx * dx + dy * dy + dz * dz);
    }
  }
  return e;
}

void lb_setup(int argc, char **argv) { steps = argc > 0 ? atol(argv[0]) : 1000000; }
void *lb_decode(const unsigned char *data, size_t len) { (void)len; memcpy(body, data, sizeof body); return body; }
void *lb_solve(void *inst) {
  (void)inst;
  double px = 0, py = 0, pz = 0;
  for (int i = 0; i < 5; i++) { px += body[i][3] * body[i][6]; py += body[i][4] * body[i][6]; pz += body[i][5] * body[i][6]; }
  body[0][3] = -px / SOLAR_MASS; body[0][4] = -py / SOLAR_MASS; body[0][5] = -pz / SOLAR_MASS;
  energies[0] = energy();
  const double dt = 0.01;
  for (long s = 0; s < steps; s++) {
    for (int i = 0; i < 5; i++)
      for (int j = i + 1; j < 5; j++) {
        double dx = body[i][0] - body[j][0], dy = body[i][1] - body[j][1], dz = body[i][2] - body[j][2];
        double d2 = dx * dx + dy * dy + dz * dz, mag = dt / (d2 * sqrt(d2));
        double bm = body[i][6] * mag, cm = body[j][6] * mag;
        body[i][3] -= dx * cm; body[i][4] -= dy * cm; body[i][5] -= dz * cm;
        body[j][3] += dx * bm; body[j][4] += dy * bm; body[j][5] += dz * bm;
      }
    for (int i = 0; i < 5; i++) { body[i][0] += dt * body[i][3]; body[i][1] += dt * body[i][4]; body[i][2] += dt * body[i][5]; }
  }
  energies[1] = energy();
  return energies;
}
void lb_emit(void *res, void (*sink)(int64_t)) { double *e = res; for (int i = 0; i < 2; i++) sink((int64_t)floor(e[i] * 1e9 + 0.5)); }
void lb_release(void *inst, void *res) { (void)inst; (void)res; }
  1. Build and run.
    build
    gcc -O3 -march=native -ffp-contract=off -o /work/bin/c-14-gcc-O3-native-nbody /repo/languages/c/harness/harness.c /repo/languages/c/impl/nbody/main.c -lm -lpthread
    run
    /work/bin/c-14-gcc-O3-native-nbody 1000000
    kernel loop
    The harness reads and decodes an instance (untimed), times only solve with 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
    hyperfine runs 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
    21 kB
  2. 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).
55.6 ms62.7 ms69.7 ms76.7 ms83.8 ms2026-10-27 · AMD EPYC 7763 64-Core148 iterations · median 67.6 ms2026-10-27 · AMD EPYC 7763 64-Core148 iterations · median 67.5 ms2026-10-27 · AMD EPYC 7763 64-Core148 iterations · median 67.6 ms2026-10-27 · AMD EPYC 9V74 80-Core176 iterations · median 57.1 ms

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.

55.2 ms61.5 ms68.5 ms76.3 ms85.0 msiteration 1 (cold)last iteration
SessionCPUIterationsColdWarm medianEnd-to-endMachine factorWarm ÷ factorAvg. / peak memorySource
2026-10-27AMD EPYC 7763 64-Core Processor (4 cores)14867.8 ms67.6 ms68.2 ms1.058×63.9 ms0.4 MB / 0.4 MBActions run · raw data · commit
2026-10-27AMD EPYC 7763 64-Core Processor (4 cores)14867.7 ms67.5 ms68.3 ms1.076×62.7 ms0.4 MB / 0.4 MBActions run · raw data · commit
2026-10-27AMD EPYC 7763 64-Core Processor (4 cores)14867.7 ms67.6 ms68.5 ms1.090×62.0 ms0.5 MB / 0.5 MBActions run · raw data · commit
2026-10-27AMD EPYC 9V74 80-Core Processor (4 cores)17657.1 ms57.1 ms57.9 ms0.880×64.9 ms0.4 MB / 0.4 MBActions 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.

  1. 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.076×, 1.090×, 0.880× (click a date in the table above for the session's details). Technically: we fit log(time) = benchmark + machine across all 8 sessions at once; see the methodology.
  2. Combining sessions. Sessions, not iterations, are the independent samples. Each session's warm median divided by its machine factor (63.9 ms, 62.7 ms, 62.0 ms, 64.9 ms) is averaged geometrically: 63.4 ms on the average machine (kernel (warm)). The 95% interval 62.4 ms–64.4 ms comes from a cluster bootstrap that resamples sessions, then iterations within each.
  3. Index vs fastest. 1.00× = this time ÷ the time of the fastest entry on the same problem, c 14 gcc-O3 (63.3 ms); interval 0.98×–1.02×.
  4. How much was hardware? Raw session medians differ by a geometric factor of 1.088; after dividing out the machine factors they differ by 1.020. The overall model leaves 1.057× of unexplained noise.
  5. 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: 0.4 MB; the highest value seen was 0.5 MB.

Reproduce it

git clone https://github.com/StijnRis/language-bench.git && cd language-bench
pnpm install
pnpm lb verify c --version 14 --variant gcc-O3-native --problems nbody

Needs 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.