Fannkuch-redux
Count pancake flips over all permutations of nine elements.
What is measured
Classic Computer Language Benchmarks Game workload: for every permutation of 0..8 (generated in a fixed order) repeatedly reverse the first k+1 elements, where k is the first element, until the first element is 0. Prints an alternating-sign checksum of the flip counts and the maximum number of flips.
Pseudocode
perm1 = [0, 1, ..., n-1]; count = array of n zeros
for each permutation p of perm1 (fixed generation order):
flips = 0; q = copy of p
while q[0] != 0:
reverse q[0..q[0]] # prefix of length q[0] + 1
flips += 1
maxflips = max(maxflips, flips)
checksum += flips if permutation index is even else -flips
print checksum, maxflipsReveals: Tight loops over small integer arrays: indexing cost, bounds checks and branch prediction, with no allocation.
Caveat: Single-threaded reference algorithm (no per-core splitting of the permutation space). Dominated by small array reads and writes, so interpreters are slowest by a large factor.
Results (newest release of each language)
| Language / build | Time (× vs best) | |
|---|---|---|
| C 16 · gcc -O2 | 18.5 ms (1.01×) | |
| C 16 · gcc -O3 -march=nativedefault | 18.7 ms (1.02×) | |
| C 16 · gcc -O3 | 19.0 ms (1.03×) | |
| Go 1.27 · GOAMD64=v3 go build | 19.5 ms (1.06×) | |
| Go 1.27 · go builddefault | 19.5 ms (1.06×) | |
| Python 3.15 · python3default | 475 ms (25.9×) |
Click a language to see the source code, build flags, every session and how the number was computed.