8 programs measured with axle bench: exception_chain, lru_cache, lzw_compress, nucleotides, rabin_karp_multi, set_jaccard, sliding_window_distinct, word_freq_topk. Run time, peak RAM at compile and run, and binary size, with the exact source for each language.
Benchmarks for v0.23.0
A failure raised three calls below the handler that answers it, one iteration in sixteen over 10M iterations — the cost of a call chain that can fail when it does not, and of carrying a failure up to its handler when it does. Axle's `throw` / `catch`, a C++ exception and a Rust `Result`, same chain, same rate, same checksum.
| Variant | Run | Compile | Output | ||
|---|---|---|---|---|---|
| Run time | RAM | Compile time | RAM | Binary | |
| Rust -O2 | 37 ms | 44.9 MB | 98 ms | 139.1 MB | 3862 KB |
| Rust -O3 | 37 ms | 44.9 MB | 99 ms | 137.0 MB | 3862 KB |
| Rust -O1 | 42 ms | 44.9 MB | 100 ms | 140.9 MB | 3863 KB |
| Axle -O1 | 55 ms | 44.9 MB | 75 ms | 123.1 MB | 312 KB |
| Axle -O2 | 57 ms | 44.9 MB | 79 ms | 124.5 MB | 312 KB |
| Axle -O3 | 57 ms | 44.9 MB | 79 ms | 124.8 MB | 312 KB |
| Axle -O0 | 86 ms | 44.9 MB | 70 ms | 123.4 MB | 313 KB |
| Rust -O0 | 239 ms | 44.9 MB | 95 ms | 139.7 MB | 3867 KB |
| C++ -O3 | 1.19 s | 44.9 MB | 357 ms | 117.1 MB | 17 KB |
| C++ -O1 | 1.19 s | 44.9 MB | 354 ms | 116.8 MB | 17 KB |
| C++ -O2 | 1.20 s | 44.9 MB | 355 ms | 117.0 MB | 17 KB |
| C++ -O0 | 1.25 s | 44.9 MB | 347 ms | 112.8 MB | 17 KB |
// A failure raised three calls below the `try` that answers it, one iteration
// in sixteen, over ten million iterations: the cost of a call chain that can
// fail when it does not (the success path), and of carrying a failure up to its
// handler when it does.
//
// The three functions are kept out of line so the chain is real: inlined, the
// whole loop folds and nothing is measured. The failing iteration builds an
// exception object, as a `throw` does in every language here; what differs is
// how it travels up the chain.
//
// One line is printed: the checksum of the answers, with -1 for a failed
// iteration.
//
// Equivalent to exception_chain.{cpp,rs} — same chain, same failure rate, same
// checksum.
exception Odd : RuntimeException {
constructor(m : string) { super(m); }
}
@inline(never)
fn leaf(n : i64) : i64 ! Odd {
if (n % 16 == 0) {
throw Odd("sixteenth");
}
return n * 3 + 1;
}
@inline(never)
fn middle(n : i64) : i64 ! Odd {
return leaf(n) + 2;
}
@inline(never)
fn top(n : i64) : i64 ! Odd {
return middle(n) ^ 5;
}
@inline(never)
fn attempt(n : i64) : i64 {
try {
return top(n);
} catch e : Odd {
return -1;
}
}
fn main() : i32 {
let mut sum : i64 = 0;
let mut i : i64 = 1;
while (i <= 10000000) {
sum = sum + attempt(i);
i = i + 1;
}
println(sum);
return 0;
}LRU cache simulation — a hash map of key→slot over an intrusive doubly-linked recency list in three parallel arrays, driven by a 3M-access skewed trace so the cache earns a real hit rate. Same capacity, same trace, same list surgery, one lookup per access in all three languages.
| Variant | Run | Compile | Output | ||
|---|---|---|---|---|---|
| Run time | RAM | Compile time | RAM | Binary | |
| Axle -O2 | 120 ms | 45.8 MB | 191 ms | 126.2 MB | 311 KB |
| Axle -O3 | 125 ms | 45.8 MB | 195 ms | 125.4 MB | 311 KB |
| Axle -O1 | 128 ms | 45.8 MB | 162 ms | 127.7 MB | 311 KB |
| Rust -O3 | 194 ms | 45.8 MB | 211 ms | 165.7 MB | 3885 KB |
| Rust -O2 | 202 ms | 45.8 MB | 193 ms | 166.6 MB | 3884 KB |
| Rust -O1 | 224 ms | 45.8 MB | 186 ms | 164.4 MB | 3885 KB |
| Axle -O0 | 289 ms | 45.8 MB | 92 ms | 126.1 MB | 315 KB |
| C++ -O3 | 302 ms | 45.8 MB | 430 ms | 116.3 MB | 18 KB |
| C++ -O2 | 310 ms | 45.8 MB | 428 ms | 116.4 MB | 18 KB |
| C++ -O1 | 331 ms | 45.8 MB | 412 ms | 116.0 MB | 18 KB |
| C++ -O0 | 1.20 s | 45.8 MB | 365 ms | 112.1 MB | 60 KB |
| Rust -O0 | 1.85 s | 45.8 MB | 134 ms | 160.4 MB | 3931 KB |
// LRU cache simulation — the eviction policy in every page cache, CDN
// edge, ORM identity map and CPU-adjacent software cache.
//
// A HashMap maps key → slot, and the recency order is an intrusive
// doubly-linked list over three parallel slot arrays (key, prev, next).
// That is the standard O(1) LRU: a hit unlinks its slot and relinks it at
// the head; a miss on a full cache takes the tail slot, drops that key
// from the map, and reuses the slot for the new key.
//
// The access trace is a deterministic LCG skewed with `k = u * u / KS`, so
// a small set of hot keys dominates and the cache actually earns its hit
// rate — a uniform trace would make any LRU look like a random-eviction
// cache and measure nothing.
//
// Three lines are printed: hits, misses, evictions.
//
// Fair-play notes: same capacity, same trace, same list surgery, and one
// map lookup per access plus one `put` per miss and one `remove` per
// eviction in every language. Each language uses its own stdlib hash map,
// so its hasher is part of the measurement.
//
// Equivalent to lru_cache.{cpp,rs}.
use std::collections::HashMap;
fn main() : i32 {
let CAP : i64 = 100000;
let TRACE : i64 = 3000000;
let KS : i64 = 400000;
let mut slot_key : i64[100000] = i64[100000];
let mut prev : i32[100000] = i32[100000];
let mut next : i32[100000] = i32[100000];
let index : HashMap<i64, i64> = new HashMap<i64, i64>();
let mut head : i64 = -1;
let mut tail : i64 = -1;
let mut used : i64 = 0;
let mut hits : i64 = 0;
let mut misses : i64 = 0;
let mut evictions : i64 = 0;
let mut seed : i64 = 20260726;
let mut t : i64 = 0;
while (t < TRACE) {
seed = (seed * 48271) % 2147483647;
let u : i64 = seed % KS;
let k : i64 = (u * u) / KS;
let mut s : i64 = index.getOrDefault(k, -1);
let mut relink : bool = false;
if (s >= 0) {
hits = hits + 1;
relink = true;
} else {
misses = misses + 1;
if (used < CAP) {
s = used;
used = used + 1;
} else {
// Evict the least-recently-used slot and reuse it.
s = tail;
evictions = evictions + 1;
index.remove(slot_key[s]);
relink = true;
}
slot_key[s] = k;
index.put(k, s);
}
if (relink) {
// Unlink s from wherever it currently sits.
let p : i64 = prev[s] as i64;
let n : i64 = next[s] as i64;
if (p >= 0) {
next[p] = n as i32;
} else {
head = n;
}
if (n >= 0) {
prev[n] = p as i32;
} else {
tail = p;
}
}
// Relink s at the head — the most-recently-used end.
prev[s] = -1;
next[s] = head as i32;
if (head >= 0) {
prev[head] = s as i32;
}
head = s;
if (tail < 0) {
tail = s;
}
t = t + 1;
}
println(hits);
println(misses);
println(evictions);
return 0;
}LZW dictionary compression (GIF / TIFF / compress) over a 4M-symbol stream of long recurring runs broken by noise, growing the (prefix, symbol) map to ~1.1M live entries. One map lookup per symbol, one insert per emitted code, and a rolling checksum that pins the exact code stream.
| Variant | Run | Compile | Output | ||
|---|---|---|---|---|---|
| Run time | RAM | Compile time | RAM | Binary | |
| Axle -O2 | 199 ms | 69.1 MB | 167 ms | 130.0 MB | 312 KB |
| Axle -O3 | 199 ms | 69.3 MB | 167 ms | 126.1 MB | 312 KB |
| Axle -O1 | 202 ms | 69.3 MB | 140 ms | 125.1 MB | 312 KB |
| Rust -O3 | 243 ms | 68.2 MB | 194 ms | 161.8 MB | 3884 KB |
| Rust -O2 | 246 ms | 68.2 MB | 182 ms | 164.1 MB | 3883 KB |
| Rust -O1 | 256 ms | 68.3 MB | 172 ms | 163.2 MB | 3884 KB |
| Axle -O0 | 286 ms | 69.2 MB | 92 ms | 127.9 MB | 315 KB |
| C++ -O2 | 870 ms | 62.0 MB | 400 ms | 115.8 MB | 17 KB |
| C++ -O3 | 873 ms | 62.0 MB | 401 ms | 115.8 MB | 17 KB |
| C++ -O1 | 877 ms | 62.0 MB | 386 ms | 115.5 MB | 18 KB |
| Rust -O0 | 1.57 s | 68.3 MB | 130 ms | 161.0 MB | 3925 KB |
| C++ -O0 | 1.58 s | 62.0 MB | 352 ms | 111.6 MB | 48 KB |
// LZW compression — the dictionary coder behind GIF, TIFF and `compress`,
// and the classic "growing hash map keyed by (prefix, symbol)" workload.
//
// The input is a synthetic 27-symbol stream: four symbols out of five are
// copied from LAG positions back, the fifth is fresh LCG noise. That mix
// is what a dictionary coder actually meets — long recurring runs broken
// by novelty — and it is what makes the dictionary grow. A pure random
// stream would keep it near-empty and a purely periodic one would let it
// saturate after a few thousand entries; neither would measure a hash map.
// The stream is generated in memory, so the benchmark contains no I/O.
//
// The coder is textbook LZW: the dictionary maps
// `prefix_code * 256 + symbol` → new code, codes 0..255 are implicit, and
// on a dictionary miss the current prefix is emitted, the extended string
// is added, and the prefix restarts at the current symbol. The dictionary
// is never reset, so it grows to ~1M entries — insert pressure and probe
// depth both matter.
//
// Three lines are printed: emitted codes, final dictionary size, and a
// rolling checksum of the emitted code stream (`h = (h * 31 + code) % p`)
// which pins the exact output sequence, not just its length.
//
// Fair-play notes: same input stream, same dictionary key encoding, same
// emit rule, one map lookup per symbol and one insert per emitted code in
// all three languages, each using its own stdlib hash map.
//
// Equivalent to lzw_compress.{cpp,rs}.
use std::collections::HashMap;
fn main() : i32 {
let L : i64 = 4000000;
let ALPHA : i64 = 27;
let LAG : i64 = 4096;
let MOD : i64 = 1000000007;
let mut data : i32[4000000] = i32[4000000];
let mut seed : i64 = 13579;
let mut i : i64 = 0;
while (i < L) {
seed = (seed * 48271) % 2147483647;
if (i < LAG || seed % 5 == 0) {
data[i] = (seed % ALPHA) as i32;
} else {
data[i] = data[i - LAG];
}
i = i + 1;
}
let dict : HashMap<i64, i64> = new HashMap<i64, i64>();
let mut next_code : i64 = 256;
let mut emitted : i64 = 0;
let mut checksum : i64 = 0;
let mut cur : i64 = data[0] as i64;
let mut k : i64 = 1;
while (k < L) {
let sym : i64 = data[k] as i64;
let key : i64 = cur * 256 + sym;
let found : i64 = dict.getOrDefault(key, -1);
if (found >= 0) {
cur = found;
} else {
emitted = emitted + 1;
checksum = (checksum * 31 + cur) % MOD;
dict.put(key, next_code);
next_code = next_code + 1;
cur = sym;
}
k = k + 1;
}
// Flush the trailing prefix.
emitted = emitted + 1;
checksum = (checksum * 31 + cur) % MOD;
println(emitted);
println(next_code);
println(checksum);
return 0;
}DNA k-mer frequency counting over a large nucleotide sequence.
| Variant | Run | Compile | Output | ||
|---|---|---|---|---|---|
| Run time | RAM | Compile time | RAM | Binary | |
| Axle -O2 | 198 ms | 131.6 MB | 103 ms | 123.2 MB | 309 KB |
| Axle -O3 | 199 ms | 131.6 MB | 105 ms | 123.4 MB | 309 KB |
| Rust -O3 | 227 ms | 131.6 MB | 141 ms | 150.1 MB | 3864 KB |
| Rust -O2 | 239 ms | 131.6 MB | 139 ms | 150.0 MB | 3864 KB |
| C++ -O3 | 244 ms | 132.9 MB | 125 ms | 97.9 MB | 16 KB |
| C++ -O2 | 246 ms | 132.9 MB | 125 ms | 97.9 MB | 16 KB |
| C++ -O1 | 257 ms | 132.9 MB | 109 ms | 96.7 MB | 16 KB |
| Axle -O1 | 260 ms | 131.6 MB | 79 ms | 123.4 MB | 308 KB |
| Rust -O1 | 473 ms | 131.7 MB | 147 ms | 146.9 MB | 3866 KB |
| C++ -O0 | 642 ms | 132.9 MB | 94 ms | 90.2 MB | 16 KB |
| Axle -O0 | 1.21 s | 131.7 MB | 71 ms | 125.1 MB | 311 KB |
| Rust -O0 | 4.93 s | 131.8 MB | 106 ms | 144.6 MB | 3881 KB |
// k-mer frequency counting on a synthetic 34 M-base DNA sequence.
//
// The cost is per-element, not per-byte: the five sweeps of the 136 MB
// sequence array are sequential and fully prefetched — 680 MB total in about
// a third of a second, roughly a tenth of a core's stream bandwidth — so what
// the loops measure is the index arithmetic (the k-mer index is recomposed
// from scratch each iteration, seven shifts and seven adds for the 8-mer) plus
// the read-modify-write scatter into the count tables (4, 16, 256, 4096, 65536
// bins; the 8-mer table is 256 KiB → L2-resident).
//
// Equivalent to nucleotides.cpp / nucleotides.rs (same N, same LCG
// seed, same output format).
fn lcg(state : i32) : i32 {
return state * 1664525 + 1013904223;
}
fn main() : void {
let mut state : i32 = 12345;
// ── DNA sequence: 34 M nucleotides packed as i32 (values 0..3). ──
// Picking top 2 bits of the LCG state gives uniform [0, 3] without
// a srem.
let mut seq : i32[34000000] = i32[34000000];
for (idx of 0..34000000) {
state = lcg(state);
seq[idx] = (state >> 30) & 3;
}
// ── 1-mer counts (4 bins). Calloc-zeroed; no explicit init. ──
let mut count1 : i32[4] = i32[4];
for (idx of 0..34000000) {
count1[seq[idx]] = count1[seq[idx]] + 1;
}
// ── 2-mer counts (16 bins). Index = seq[i]*4 + seq[i+1]. ──
let mut count2 : i32[16] = i32[16];
for (idx of 0..33999999) {
let k : i32 = seq[idx] * 4 + seq[idx + 1];
count2[k] = count2[k] + 1;
}
// ── 4-mer counts (256 bins). ──
let mut count4 : i32[256] = i32[256];
for (idx of 0..33999997) {
let k : i32 = seq[idx] * 64 + seq[idx + 1] * 16 + seq[idx + 2] * 4 + seq[idx + 3];
count4[k] = count4[k] + 1;
}
// ── 6-mer counts (4096 bins, 16 KiB). ──
let mut count6 : i32[4096] = i32[4096];
for (idx of 0..33999995) {
let k : i32 = seq[idx] * 1024 + seq[idx + 1] * 256 + seq[idx + 2] * 64 + seq[idx + 3] * 16 + seq[idx + 4] * 4 + seq[idx + 5];
count6[k] = count6[k] + 1;
}
// ── 8-mer counts (65536 bins, 256 KiB → L2). ──
let mut count8 : i32[65536] = i32[65536];
for (idx of 0..33999993) {
let k : i32 = seq[idx] * 16384 + seq[idx + 1] * 4096 + seq[idx + 2] * 1024 + seq[idx + 3] * 256 + seq[idx + 4] * 64 + seq[idx + 5] * 16 + seq[idx + 6] * 4 + seq[idx + 7];
count8[k] = count8[k] + 1;
}
// ── Verification sums (each must equal the loop trip count). ──
let mut sum1 : i32 = 0;
for (i of 0..4) {
sum1 = sum1 + count1[i];
}
println(sum1);
let mut sum2 : i32 = 0;
for (i of 0..16) {
sum2 = sum2 + count2[i];
}
println(sum2);
let mut sum4 : i32 = 0;
for (i of 0..256) {
sum4 = sum4 + count4[i];
}
println(sum4);
let mut sum6 : i32 = 0;
for (i of 0..4096) {
sum6 = sum6 + count6[i];
}
println(sum6);
let mut sum8 : i32 = 0;
for (i of 0..65536) {
sum8 = sum8 + count8[i];
}
println(sum8);
// Specific motif counts — deterministic across implementations.
println(count1[0]);
println(count2[5]);
println(count4[100]);
println(count6[1000]);
println(count8[12345]);
println("Done!");
}Multi-pattern Rabin–Karp over an 8M-symbol text against a hash set of 20000 window fingerprints — how rsync matches blocks and scanners screen signatures. One modular rolling-hash update plus one set probe per text position; patterns are sampled from the text itself so every one is guaranteed found.
| Variant | Run | Compile | Output | ||
|---|---|---|---|---|---|
| Run time | RAM | Compile time | RAM | Binary | |
| Axle -O2 | 117 ms | 44.3 MB | 174 ms | 126.2 MB | 312 KB |
| Axle -O3 | 118 ms | 44.3 MB | 207 ms | 126.0 MB | 314 KB |
| Axle -O1 | 119 ms | 44.3 MB | 134 ms | 125.9 MB | 312 KB |
| Rust -O3 | 180 ms | 44.3 MB | 201 ms | 161.5 MB | 3886 KB |
| Rust -O2 | 184 ms | 44.3 MB | 192 ms | 163.5 MB | 3885 KB |
| Rust -O1 | 192 ms | 44.3 MB | 179 ms | 164.8 MB | 3885 KB |
| C++ -O3 | 312 ms | 44.3 MB | 414 ms | 115.3 MB | 22 KB |
| C++ -O2 | 313 ms | 44.3 MB | 410 ms | 115.2 MB | 22 KB |
| C++ -O1 | 371 ms | 44.3 MB | 389 ms | 114.9 MB | 18 KB |
| Axle -O0 | 383 ms | 44.3 MB | 87 ms | 128.1 MB | 315 KB |
| C++ -O0 | 1.12 s | 44.3 MB | 347 ms | 111.1 MB | 53 KB |
| Rust -O0 | 1.77 s | 44.3 MB | 125 ms | 162.3 MB | 3927 KB |
// Multi-pattern Rabin–Karp search — one rolling hash over the text against
// a HashSet of pattern fingerprints. This is how rsync finds matching
// blocks, how plagiarism and near-duplicate detectors shingle documents,
// and how a scanner screens thousands of signatures in a single pass.
//
// A polynomial hash `h = (h * BASE + symbol) mod P` over a window of W
// symbols is rolled across the text: the outgoing symbol is subtracted
// (scaled by BASE^(W-1), precomputed by fast exponentiation) and the
// incoming one folded in. Every window fingerprint is probed against the
// set — so the cost is one modular update plus one hash-set probe per text
// position, which is the real shape of the algorithm.
//
// The patterns are windows sampled out of the text itself at LCG-chosen
// positions, so every one of them is guaranteed to be found: the hit count
// is a property of the text, not of luck.
//
// Two lines are printed: how many distinct fingerprints the pattern set
// holds, and how many text positions matched one.
//
// Fair-play notes: same text, same patterns, same hash constants, same
// probe per position, each language on its own stdlib hash set.
//
// Equivalent to rabin_karp_multi.{cpp,rs}.
use std::collections::HashSet;
fn main() : i32 {
let L : i64 = 8000000;
let W : i64 = 16;
let PATTERNS : i64 = 20000;
let BASE : i64 = 257;
let P : i64 = 1000000007;
let mut text : i32[8000000] = i32[8000000];
let mut seed : i64 = 8675309;
let mut i : i64 = 0;
while (i < L) {
seed = (seed * 48271) % 2147483647;
text[i] = (seed % 256) as i32;
i = i + 1;
}
// BASE^(W-1) mod P — the weight of the symbol leaving the window.
let mut power : i64 = 1;
let mut e : i64 = 0;
while (e < W - 1) {
power = (power * BASE) % P;
e = e + 1;
}
// Fingerprints of W-symbol windows sampled from the text itself.
let patterns : HashSet<i64> = new HashSet<i64>();
let mut pseed : i64 = 1234567;
let mut p : i64 = 0;
while (p < PATTERNS) {
pseed = (pseed * 48271) % 2147483647;
let start : i64 = pseed % (L - W);
let mut h : i64 = 0;
let mut k : i64 = 0;
while (k < W) {
h = (h * BASE + (text[start + k] as i64)) % P;
k = k + 1;
}
patterns.add(h);
p = p + 1;
}
// Roll one window across the whole text, probing every position.
let mut hash : i64 = 0;
let mut j : i64 = 0;
while (j < W) {
hash = (hash * BASE + (text[j] as i64)) % P;
j = j + 1;
}
let mut hits : i64 = 0;
if (patterns.contains(hash)) {
hits = hits + 1;
}
let mut pos : i64 = W;
while (pos < L) {
let out : i64 = text[pos - W] as i64;
hash = (hash - out * power % P + P) % P;
hash = (hash * BASE + (text[pos] as i64)) % P;
if (patterns.contains(hash)) {
hits = hits + 1;
}
pos = pos + 1;
}
println(patterns.size());
println(hits);
return 0;
}Dedup plus Jaccard similarity of two 3M-element id streams — near-duplicate detection and audience overlap. Two hash sets absorb the repeats, then stream B is replayed against set A to count the overlap exactly once per element (no set is ever iterated, so all three languages probe from the source stream).
| Variant | Run | Compile | Output | ||
|---|---|---|---|---|---|
| Run time | RAM | Compile time | RAM | Binary | |
| Axle -O2 | 353 ms | 53.5 MB | 151 ms | 125.9 MB | 310 KB |
| Axle -O3 | 354 ms | 53.4 MB | 153 ms | 126.0 MB | 310 KB |
| Axle -O1 | 365 ms | 53.4 MB | 127 ms | 126.1 MB | 310 KB |
| Rust -O3 | 540 ms | 55.7 MB | 176 ms | 161.1 MB | 3884 KB |
| Rust -O2 | 553 ms | 55.9 MB | 170 ms | 161.4 MB | 3883 KB |
| Rust -O1 | 558 ms | 55.7 MB | 163 ms | 160.4 MB | 3884 KB |
| Axle -O0 | 688 ms | 53.4 MB | 86 ms | 126.1 MB | 313 KB |
| C++ -O2 | 1.50 s | 126.1 MB | 337 ms | 111.3 MB | 18 KB |
| C++ -O3 | 1.50 s | 126.1 MB | 334 ms | 111.3 MB | 18 KB |
| C++ -O1 | 1.79 s | 126.1 MB | 323 ms | 110.9 MB | 18 KB |
| Rust -O0 | 3.63 s | 55.8 MB | 118 ms | 154.2 MB | 3921 KB |
| C++ -O0 | 4.18 s | 126.1 MB | 281 ms | 107.2 MB | 48 KB |
// Deduplication + Jaccard similarity of two large id streams — the shape
// of near-duplicate detection, audience overlap, and "which shingles do
// these two documents share".
//
// Two deterministic LCG streams are deduplicated into HashSets (that is
// the dedup half: most adds are rejected repeats). The intersection is
// then counted by probing every member of the second stream's *source*
// order against set A, guarded by a membership check in B so each distinct
// element is counted once — no set can be walked in Axle, so all three
// languages probe from the source stream instead of iterating a set. From
// |A|, |B| and |A ∩ B| the Jaccard numerator and denominator follow.
//
// Four lines are printed: |A|, |B|, |A ∩ B| and |A ∪ B|.
//
// Fair-play notes: same streams, same probe order, same probe counts, and
// each language uses its own stdlib hash set — the hasher is part of what
// is compared.
//
// Equivalent to set_jaccard.{cpp,rs}.
use std::collections::HashSet;
fn main() : i32 {
let NA : i64 = 3000000;
let NB : i64 = 3000000;
let SPACE_A : i64 = 1500000;
let SPACE_B : i64 = 1500000;
let OFFSET : i64 = 750000;
let a : HashSet<i64> = new HashSet<i64>();
let mut seed : i64 = 11111;
let mut i : i64 = 0;
while (i < NA) {
seed = (seed * 48271) % 2147483647;
a.add(seed % SPACE_A);
i = i + 1;
}
let b : HashSet<i64> = new HashSet<i64>();
let mut seed_b : i64 = 22222;
let mut j : i64 = 0;
while (j < NB) {
seed_b = (seed_b * 48271) % 2147483647;
b.add(OFFSET + seed_b % SPACE_B);
j = j + 1;
}
// Replay stream B and count each element of the overlap once. The
// `counted` set is what makes "once" precise without iterating B.
let counted : HashSet<i64> = new HashSet<i64>();
let mut overlap : i64 = 0;
seed_b = 22222;
let mut k : i64 = 0;
while (k < NB) {
seed_b = (seed_b * 48271) % 2147483647;
let v : i64 = OFFSET + seed_b % SPACE_B;
if (a.contains(v) && !counted.contains(v)) {
counted.add(v);
overlap = overlap + 1;
}
k = k + 1;
}
let union_size : i64 = a.size() + b.size() - overlap;
println(a.size());
println(b.size());
println(overlap);
println(union_size);
return 0;
}Longest window with at most K distinct symbols over a 5M-symbol stream — two pointers plus a live multiset of counts. Every symbol enters and leaves the map exactly once, so this is the rare collection bench that hits the insert, update AND delete paths in one loop.
| Variant | Run | Compile | Output | ||
|---|---|---|---|---|---|
| Run time | RAM | Compile time | RAM | Binary | |
| Axle -O2 | 147 ms | 45.5 MB | 185 ms | 127.4 MB | 312 KB |
| Axle -O3 | 151 ms | 45.5 MB | 194 ms | 128.2 MB | 313 KB |
| Axle -O1 | 153 ms | 45.5 MB | 150 ms | 126.0 MB | 312 KB |
| Rust -O3 | 249 ms | 45.5 MB | 204 ms | 162.1 MB | 3885 KB |
| Rust -O2 | 259 ms | 45.5 MB | 187 ms | 164.3 MB | 3884 KB |
| Rust -O1 | 304 ms | 45.5 MB | 179 ms | 166.1 MB | 3884 KB |
| Axle -O0 | 315 ms | 45.5 MB | 93 ms | 126.1 MB | 316 KB |
| C++ -O3 | 319 ms | 45.5 MB | 428 ms | 115.9 MB | 17 KB |
| C++ -O2 | 329 ms | 45.5 MB | 428 ms | 115.9 MB | 18 KB |
| C++ -O1 | 447 ms | 45.5 MB | 405 ms | 115.6 MB | 18 KB |
| C++ -O0 | 2.44 s | 45.5 MB | 354 ms | 111.7 MB | 54 KB |
| Rust -O0 | 4.25 s | 45.5 MB | 126 ms | 159.9 MB | 3927 KB |
// Longest window containing at most K distinct symbols — the two-pointer
// + multiset-count pattern behind rate limiters, session windows, and the
// "longest substring with at most K distinct characters" family.
//
// A HashMap holds the live count of every symbol inside the window. The
// right edge always advances; whenever the distinct count exceeds K the
// left edge advances, decrementing counts and erasing the entry that
// reaches zero. Each symbol therefore enters and leaves the map exactly
// once, which is what makes the scan linear — and it exercises the map's
// insert, update AND delete paths in one loop, which insert-only
// benchmarks never do.
//
// The stream is a deterministic LCG over a 4096-symbol alphabet, so the
// window hovers near its steady-state size instead of degenerating to the
// whole input.
//
// Three lines are printed: the best window length, where it starts, and
// how many map entries were erased (the delete-path count).
//
// Fair-play notes: same stream, same K, same pointer discipline, same
// per-step probe counts, each language on its own stdlib hash map.
//
// Equivalent to sliding_window_distinct.{cpp,rs}.
use std::collections::HashMap;
fn main() : i32 {
let T : i64 = 5000000;
let ALPHA : i64 = 4096;
let K : i64 = 1000;
let mut data : i32[5000000] = i32[5000000];
let mut seed : i64 = 31415927;
let mut i : i64 = 0;
while (i < T) {
seed = (seed * 48271) % 2147483647;
data[i] = (seed % ALPHA) as i32;
i = i + 1;
}
let counts : HashMap<i64, i64> = new HashMap<i64, i64>();
let mut distinct : i64 = 0;
let mut removals : i64 = 0;
let mut best_len : i64 = 0;
let mut best_start : i64 = 0;
let mut left : i64 = 0;
let mut right : i64 = 0;
while (right < T) {
let s : i64 = data[right] as i64;
let c : i64 = counts.getOrDefault(s, 0);
if (c == 0) {
distinct = distinct + 1;
}
counts.put(s, c + 1);
while (distinct > K) {
let ls : i64 = data[left] as i64;
let lc : i64 = counts.getOrDefault(ls, 0);
if (lc == 1) {
counts.remove(ls);
distinct = distinct - 1;
removals = removals + 1;
} else {
counts.put(ls, lc - 1);
}
left = left + 1;
}
let len : i64 = right - left + 1;
if (len > best_len) {
best_len = len;
best_start = left;
}
right = right + 1;
}
println(best_len);
println(best_start);
println(removals);
return 0;
}Word-frequency counting plus top-K extraction over a 3M-token Zipf-skewed stream — the tally under every log analyser and tf-idf pipeline. Two map probes per token (read the count, write count+1) with the key stored only on first sight, identical in all three languages; each uses its own stdlib string-keyed map, so its real hasher is part of the number.
| Variant | Run | Compile | Output | ||
|---|---|---|---|---|---|
| Run time | RAM | Compile time | RAM | Binary | |
| Axle -O3 | 829 ms | 44.1 MB | 263 ms | 125.8 MB | 323 KB |
| Axle -O2 | 843 ms | 44.1 MB | 228 ms | 127.9 MB | 322 KB |
| Axle -O1 | 922 ms | 44.1 MB | 177 ms | 126.0 MB | 322 KB |
| C++ -O1 | 1.10 s | 44.1 MB | 624 ms | 123.7 MB | 19 KB |
| C++ -O3 | 1.14 s | 44.1 MB | 666 ms | 124.1 MB | 23 KB |
| C++ -O2 | 1.15 s | 44.1 MB | 646 ms | 124.2 MB | 23 KB |
| Rust -O2 | 1.29 s | 44.1 MB | 196 ms | 176.2 MB | 3890 KB |
| Rust -O3 | 1.31 s | 44.1 MB | 198 ms | 180.1 MB | 3890 KB |
| Rust -O1 | 1.37 s | 44.1 MB | 183 ms | 177.9 MB | 3892 KB |
| Axle -O0 | 1.46 s | 44.1 MB | 92 ms | 125.9 MB | 326 KB |
| C++ -O0 | 2.17 s | 44.1 MB | 550 ms | 120.6 MB | 91 KB |
| Rust -O0 | 2.92 s | 44.1 MB | 133 ms | 169.3 MB | 3946 KB |
// Word-frequency counting + top-K extraction — the tally at the bottom of
// every log analyser, search indexer and tf-idf pipeline.
//
// Phase 1 builds a fixed vocabulary of `V` distinct word strings up front,
// so the measured loop is the map algorithm and not string formatting.
// Phase 2 streams `T` tokens drawn from that vocabulary by a deterministic
// LCG, skewed with `idx = u * u / V` so the distribution is Zipf-like
// (a few very frequent words, a long tail) exactly as real text is.
// Phase 3 extracts the K most frequent words by repeated max-scan over the
// first-seen index list.
//
// Fair-play notes — all three languages perform the same actions:
// * the vocabulary is materialised identically (one owned string per
// index) before timing-relevant work starts;
// * every token does exactly TWO map probes on the hot path — one to
// read the current count, one to write count+1 — and stores the key
// only the first time it is seen. No language gets a fused
// "increment in place" fast path the others lack;
// * each language uses its OWN stdlib hash container (Axle's HashMap,
// `std::unordered_map`, Rust's `HashMap`), so the number includes each
// one's real hasher — that is the comparison being made, not a
// hand-rolled table shared between them;
// * the top-K scan walks the first-seen list in insertion order and
// breaks ties with a strict `>`, so the winners are identical.
//
// Three lines are printed: distinct words, the top word's count, and the
// summed count of the top K.
//
// Equivalent to word_freq_topk.{cpp,rs}.
use std::collections::HashMap;
fn main() : i32 {
let V : i64 = 200000;
let T : i64 = 3000000;
let K : i64 = 10;
// The vocabulary — one owned string per index, built once.
let mut words : string[] = new string[200000];
let mut p : i64 = 0;
while (p < V) {
words[p] = p.toString();
p = p + 1;
}
let counts : HashMap<string, i64> = new HashMap<string, i64>();
// Vocabulary indices in first-seen order — the map cannot be walked,
// so the top-K phase needs this list. Every language keeps it.
let mut seen : i64[200000] = i64[200000];
let mut nseen : i64 = 0;
let mut seed : i64 = 12345;
let mut t : i64 = 0;
while (t < T) {
seed = (seed * 48271) % 2147483647;
let u : i64 = seed % V;
let idx : i64 = (u * u) / V;
let w : string = words[idx];
let c : i64 = counts.getOrDefault(w, 0);
if (c == 0) {
seen[nseen] = idx;
nseen = nseen + 1;
}
counts.put(w, c + 1);
t = t + 1;
}
// Top-K by repeated max-scan; `taken` retires a winner between rounds.
let mut taken : bool[200000] = bool[200000];
let mut top1 : i64 = 0;
let mut top_sum : i64 = 0;
let mut k : i64 = 0;
while (k < K) {
let mut best_count : i64 = -1;
let mut best_idx : i64 = 0;
let mut i : i64 = 0;
while (i < nseen) {
let vi : i64 = seen[i];
if (!taken[vi]) {
let c : i64 = counts.getOrDefault(words[vi], 0);
if (c > best_count) {
best_count = c;
best_idx = vi;
}
}
i = i + 1;
}
taken[best_idx] = true;
if (k == 0) {
top1 = best_count;
}
top_sum = top_sum + best_count;
k = k + 1;
}
println(counts.size());
println(top1);
println(top_sum);
return 0;
}