Locality benchmark
The brief claims a column-major walk is many times slower than a row-major one over the same data, and that the gap is cache lines. This runs all three passes and prints the ratios. The third, which visits the same columns in a scrambled order, is the check on that second claim: it touches the same bytes as the plain column walk, so if the plain walk were already discarding most of each fetched line, scrambling would cost little more. It costs over twice as much again, which is only possible if the plain walk was reusing lines the scrambled one cannot. The environment below is not decoration: the array has to be far larger than the caches and far smaller than free memory, and the earlier version of this measurement got that wrong in a way the machine’s memory size would have exposed.
THE SOURCE
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
THE RUN
$ cc --version
Apple clang version 17.0.0 (clang-1700.6.4.2)
Target: arm64-apple-darwin25.6.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin$ sysctl -n machdep.cpu.brand_string
Apple M3$ sysctl -n hw.memsize
17179869184$ sysctl -n hw.perflevel0.l2cachesize
16777216$ cc -O2 -Wall -Wextra -o bench locality-benchmark.c && ./bench
13000 x 13000 ints, 645 MiB, best of 24
row-major 0.011 s 0.064 ns/elem
column-major 0.301 s 1.780 ns/elem
column-major, scrambled 0.645 s 3.818 ns/elem
column-major / row-major 28.02x slower
scrambled / column-major 2.15x slowerNEXT
This file accompanies When CPUs Stopped Scaling: Why Hardware Got Complicated, which is where the claim it checks is made.
ALL SOURCE EXHIBITS →