Benchmarks

Algorithms benchmarks

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

Algorithms

exception_chain

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.

VariantRunCompileOutput
Run timeRAMCompile timeRAMBinary
Rust -O237 ms44.9 MB98 ms139.1 MB3862 KB
Rust -O337 ms44.9 MB99 ms137.0 MB3862 KB
Rust -O142 ms44.9 MB100 ms140.9 MB3863 KB
Axle -O155 ms44.9 MB75 ms123.1 MB312 KB
Axle -O257 ms44.9 MB79 ms124.5 MB312 KB
Axle -O357 ms44.9 MB79 ms124.8 MB312 KB
Axle -O086 ms44.9 MB70 ms123.4 MB313 KB
Rust -O0239 ms44.9 MB95 ms139.7 MB3867 KB
C++ -O31.19 s44.9 MB357 ms117.1 MB17 KB
C++ -O11.19 s44.9 MB354 ms116.8 MB17 KB
C++ -O21.20 s44.9 MB355 ms117.0 MB17 KB
C++ -O01.25 s44.9 MB347 ms112.8 MB17 KB
▸ Source
// 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

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.

VariantRunCompileOutput
Run timeRAMCompile timeRAMBinary
Axle -O2120 ms45.8 MB191 ms126.2 MB311 KB
Axle -O3125 ms45.8 MB195 ms125.4 MB311 KB
Axle -O1128 ms45.8 MB162 ms127.7 MB311 KB
Rust -O3194 ms45.8 MB211 ms165.7 MB3885 KB
Rust -O2202 ms45.8 MB193 ms166.6 MB3884 KB
Rust -O1224 ms45.8 MB186 ms164.4 MB3885 KB
Axle -O0289 ms45.8 MB92 ms126.1 MB315 KB
C++ -O3302 ms45.8 MB430 ms116.3 MB18 KB
C++ -O2310 ms45.8 MB428 ms116.4 MB18 KB
C++ -O1331 ms45.8 MB412 ms116.0 MB18 KB
C++ -O01.20 s45.8 MB365 ms112.1 MB60 KB
Rust -O01.85 s45.8 MB134 ms160.4 MB3931 KB
▸ Source
// 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_compress

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.

VariantRunCompileOutput
Run timeRAMCompile timeRAMBinary
Axle -O2199 ms69.1 MB167 ms130.0 MB312 KB
Axle -O3199 ms69.3 MB167 ms126.1 MB312 KB
Axle -O1202 ms69.3 MB140 ms125.1 MB312 KB
Rust -O3243 ms68.2 MB194 ms161.8 MB3884 KB
Rust -O2246 ms68.2 MB182 ms164.1 MB3883 KB
Rust -O1256 ms68.3 MB172 ms163.2 MB3884 KB
Axle -O0286 ms69.2 MB92 ms127.9 MB315 KB
C++ -O2870 ms62.0 MB400 ms115.8 MB17 KB
C++ -O3873 ms62.0 MB401 ms115.8 MB17 KB
C++ -O1877 ms62.0 MB386 ms115.5 MB18 KB
Rust -O01.57 s68.3 MB130 ms161.0 MB3925 KB
C++ -O01.58 s62.0 MB352 ms111.6 MB48 KB
▸ Source
// 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;
}

nucleotides

DNA k-mer frequency counting over a large nucleotide sequence.

VariantRunCompileOutput
Run timeRAMCompile timeRAMBinary
Axle -O2198 ms131.6 MB103 ms123.2 MB309 KB
Axle -O3199 ms131.6 MB105 ms123.4 MB309 KB
Rust -O3227 ms131.6 MB141 ms150.1 MB3864 KB
Rust -O2239 ms131.6 MB139 ms150.0 MB3864 KB
C++ -O3244 ms132.9 MB125 ms97.9 MB16 KB
C++ -O2246 ms132.9 MB125 ms97.9 MB16 KB
C++ -O1257 ms132.9 MB109 ms96.7 MB16 KB
Axle -O1260 ms131.6 MB79 ms123.4 MB308 KB
Rust -O1473 ms131.7 MB147 ms146.9 MB3866 KB
C++ -O0642 ms132.9 MB94 ms90.2 MB16 KB
Axle -O01.21 s131.7 MB71 ms125.1 MB311 KB
Rust -O04.93 s131.8 MB106 ms144.6 MB3881 KB
▸ Source
// 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!");
}

rabin_karp_multi

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.

VariantRunCompileOutput
Run timeRAMCompile timeRAMBinary
Axle -O2117 ms44.3 MB174 ms126.2 MB312 KB
Axle -O3118 ms44.3 MB207 ms126.0 MB314 KB
Axle -O1119 ms44.3 MB134 ms125.9 MB312 KB
Rust -O3180 ms44.3 MB201 ms161.5 MB3886 KB
Rust -O2184 ms44.3 MB192 ms163.5 MB3885 KB
Rust -O1192 ms44.3 MB179 ms164.8 MB3885 KB
C++ -O3312 ms44.3 MB414 ms115.3 MB22 KB
C++ -O2313 ms44.3 MB410 ms115.2 MB22 KB
C++ -O1371 ms44.3 MB389 ms114.9 MB18 KB
Axle -O0383 ms44.3 MB87 ms128.1 MB315 KB
C++ -O01.12 s44.3 MB347 ms111.1 MB53 KB
Rust -O01.77 s44.3 MB125 ms162.3 MB3927 KB
▸ Source
// 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;
}

set_jaccard

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

VariantRunCompileOutput
Run timeRAMCompile timeRAMBinary
Axle -O2353 ms53.5 MB151 ms125.9 MB310 KB
Axle -O3354 ms53.4 MB153 ms126.0 MB310 KB
Axle -O1365 ms53.4 MB127 ms126.1 MB310 KB
Rust -O3540 ms55.7 MB176 ms161.1 MB3884 KB
Rust -O2553 ms55.9 MB170 ms161.4 MB3883 KB
Rust -O1558 ms55.7 MB163 ms160.4 MB3884 KB
Axle -O0688 ms53.4 MB86 ms126.1 MB313 KB
C++ -O21.50 s126.1 MB337 ms111.3 MB18 KB
C++ -O31.50 s126.1 MB334 ms111.3 MB18 KB
C++ -O11.79 s126.1 MB323 ms110.9 MB18 KB
Rust -O03.63 s55.8 MB118 ms154.2 MB3921 KB
C++ -O04.18 s126.1 MB281 ms107.2 MB48 KB
▸ Source
// 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;
}

sliding_window_distinct

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.

VariantRunCompileOutput
Run timeRAMCompile timeRAMBinary
Axle -O2147 ms45.5 MB185 ms127.4 MB312 KB
Axle -O3151 ms45.5 MB194 ms128.2 MB313 KB
Axle -O1153 ms45.5 MB150 ms126.0 MB312 KB
Rust -O3249 ms45.5 MB204 ms162.1 MB3885 KB
Rust -O2259 ms45.5 MB187 ms164.3 MB3884 KB
Rust -O1304 ms45.5 MB179 ms166.1 MB3884 KB
Axle -O0315 ms45.5 MB93 ms126.1 MB316 KB
C++ -O3319 ms45.5 MB428 ms115.9 MB17 KB
C++ -O2329 ms45.5 MB428 ms115.9 MB18 KB
C++ -O1447 ms45.5 MB405 ms115.6 MB18 KB
C++ -O02.44 s45.5 MB354 ms111.7 MB54 KB
Rust -O04.25 s45.5 MB126 ms159.9 MB3927 KB
▸ Source
// 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_freq_topk

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.

VariantRunCompileOutput
Run timeRAMCompile timeRAMBinary
Axle -O3829 ms44.1 MB263 ms125.8 MB323 KB
Axle -O2843 ms44.1 MB228 ms127.9 MB322 KB
Axle -O1922 ms44.1 MB177 ms126.0 MB322 KB
C++ -O11.10 s44.1 MB624 ms123.7 MB19 KB
C++ -O31.14 s44.1 MB666 ms124.1 MB23 KB
C++ -O21.15 s44.1 MB646 ms124.2 MB23 KB
Rust -O21.29 s44.1 MB196 ms176.2 MB3890 KB
Rust -O31.31 s44.1 MB198 ms180.1 MB3890 KB
Rust -O11.37 s44.1 MB183 ms177.9 MB3892 KB
Axle -O01.46 s44.1 MB92 ms125.9 MB326 KB
C++ -O02.17 s44.1 MB550 ms120.6 MB91 KB
Rust -O02.92 s44.1 MB133 ms169.3 MB3946 KB
▸ Source
// 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;
}