Skip to content

About

The same small programs in several compiled languages, built and benchmarked the same way (macOS arm64)

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

5 Commits

Folders and files

Repository files navigation

language-benches

The same small programs written in several compiled languages, built the same way and compared. Target: macOS arm64.

./run.sh                     # every test
./run.sh basic/hello-world   # one or more tests

Layout

<category>/<test>/
  expected.txt        exact stdout every language must print
  args                optional: command-line arguments passed to every program
  <lang>/main.<ext>   one directory per language (missing = skipped)
lib/langs.sh          build commands and version lookup per language
dashboard/index.php   results dashboard (reads build/)
dashboard-start.sh    serve the dashboard in the background
dashboard-stop.sh     stop it
build/                output, regenerated by run.sh:
  <category>/<test>/<profile>/<lang>   binaries
  <category>/<test>/sizes.csv          binary size, linked libraries, compile time
  <category>/<test>/times.csv          hyperfine stats (every test)
  toolchains.csv                       compiler versions used

Languages: asm c cpp zig rust swift go. Profiles: release (the usual optimized build) and min (size-optimized and stripped). run.sh builds every language in each profile, checks its stdout against expected.txt, and prints binary sizes and build times. It stops at the first program whose output differs.

Every test is timed: the release builds are run with hyperfine (brew install hyperfine) and ranked by median wall time, with peak memory. Full stats land in build/<category>/<test>/times.csv. basic/hello-world is short enough that this is process startup. Compile time (wall-clock milliseconds of each build) is printed in the size table and stored as build_ms, the last column of sizes.csv; the dashboard charts it next to binary size. Pass problem sizes through args rather than hard-coding them, so compilers can't precompute the answer. Close busy apps first; background load (e.g. Spotlight indexing) skews times.

Dashboard

./dashboard-start.sh             # http://127.0.0.1:8765
PORT=9000 ./dashboard-start.sh   # another port
./dashboard-stop.sh

The server runs in the background until stopped; its pid and log live in build/dashboard.pid and build/dashboard.log. Starting twice reports the running copy instead of starting another.

Shows the latest results from build/ with charts for binary size, build time, run time and peak memory, a table view per test, and toolchain versions. It is read-only: rerun ./run.sh and reload to refresh. Needs PHP 8.1+ (brew install php).

Tests

test measures size (args)
basic/hello-world binary size floor, runtime linkage, startup none
speed/fib-recursive function-call overhead: naive recursive fib fib(40)
speed/n-body floating point 30,000,000 steps
speed/sieve array loops and bounds checks primes below 200,000,000
speed/mandelbrot floating point with branches 4000 × 4000
speed/matrix-multiply cache behaviour and auto-vectorisation 1600 × 1600
memory/binary-trees allocation and GC/ARC/malloc cost depth 18
memory/hash-map hash map inserts and lookups 80,000,000 ops on 1,000,000 keys

Assembly is written only for hello-world, fib-recursive and sieve. hash-map uses each language's standard map, except C, which has none: it uses a hand-written open-addressing table.

Results

A snapshot from 2026-11-10 on an Apple M5 Max (macOS 27.3), with Apple clang 21.0.0, Zig 0.17.1, Rust 1.98.1, Swift 6.4 and Go 1.27.3. Your machine will differ: run ./run.sh and open the dashboard for your own numbers.

Median wall time of the release builds in milliseconds (lower is better, fastest in bold, – means no version in that language):

test asm C C++ Zig Rust Swift Go
basic/hello-world 1.0 1.1 1.1 1.4 1.4 1.3 1.9
speed/fib-recursive 296 177 158 162 165 257 237
speed/n-body – 874 871 599 890 1098 941
speed/sieve 801 687 692 738 831 893 769
speed/mandelbrot – 441 427 599 565 573 660
speed/matrix-multiply – 571 493 1175 504 2064 1390
memory/binary-trees – 1065 1265 1056 1016 1537 617
memory/hash-map – 491 1967 1053 2373 1988 3903

hello-world binary size in KiB:

profile asm C C++ Swift Zig Rust Go
release 16.5 32.6 35.1 49.5 387.5 423.6 2373
min 16.4 32.6 34.4 49.2 162.3 279.2 1534

Worth knowing when reading these:

  • Zig's compiler turned the n-body maths into SIMD instructions (208 vector maths instructions in its binary, against 11 in C's), which is why it wins there. On matrix-multiply C, C++ and Rust vectorised the inner loop and Zig, Swift and Go did not.
  • Go is fastest at binary-trees but peaks at 40.6 MiB of memory, against about 18 MiB for C, C++, Zig and Rust. Swift peaks at 34 MiB.
  • C's hash-map is a hand-written table, so it is not a standard-library comparison. Rust's default HashMap hasher (SipHash) trades speed for resistance to hash-flooding attacks; how much that costs here is not measured.
  • Average build time per test (release / min): C about 60 ms, asm about 70 ms, Go 183 / 125 ms, Rust 237 ms / 1.6 s (fat LTO), C++ and Swift about 330 ms, Zig 5.7 / 1.1 s. Each build runs once per run, so these are rougher than the run times.

Adding a test

  1. Make <category>/<test>/expected.txt.
  2. Add <lang>/main.<ext> for each language you want to compare.
  3. ./run.sh <category>/<test>

Keep the algorithm and data layout the same in every language, so the comparison is between compilers and runtimes rather than between programs. Print integers, or floats with few enough decimals that small rounding differences (for example from fused multiply-add) don't change the output. In Swift, put the work in a function: variables in top-level main.swift code are globals, and every access is checked at run time, which made matrix-multiply about 2x slower.

Adding a language

Add it to LANGS and give it one compile case per profile and a toolchain_version case in lib/langs.sh. Add its display name to LANG_NAMES in dashboard/index.php.

About

The same small programs in several compiled languages, built and benchmarked the same way (macOS arm64)

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages