The benchmark compares the existing specialized Bigrams, Trigrams,
Tetragrams, and Pentagrams scoring loops with NGramModel on the same
float32 score table and the same numeric candidate texts. It uses synthetic
data so that every run is reproducible without downloading a corpus. It is not
a language-quality evaluation or a promise for another CPU/workload.
Default candidate lengths are 16, 64, 256 and 4096 tokens. The 16-token case deliberately exposes fixed NumPy/API overhead for very short candidates rather than hiding it behind large batches.
Run from the repository root after installation:
python benchmarks/benchmark_models.py --output benchmarks/results.json --report benchmarks/RESULTS.mdOr run without installing, on Linux/macOS:
PYTHONPATH=src python benchmarks/benchmark_models.pyThe script records Python, NumPy, CPU information, parameters, warmups,
calibrated loop counts, individual samples and median timings. Stop unrelated
CPU-heavy work before collecting the final results. Default repeated timings
are short enough for a local check; increase --repeats and --target-ms for
a more stable performance investigation.
Each timing round rotates the implementation order. This limits bias from time-varying machine load; it cannot eliminate noise on a shared machine. The JSON also records SHA-256 fingerprints of the scoring sources and benchmark script. A run refuses to publish results if those files change during timing.
- Complete numeric candidate scoring: input is an ordinary list of
integer token IDs, as in the old library. Every call includes key/window
preparation used by that implementation. This is the principal comparison
for search algorithms repeatedly evaluating candidate decryptions.
The explicit
as_legacy_grams(copy=False)adapter is also measured after verifying that it uses the very same legacy method and shared score table. - Prepared scalar lookup: tensor index tuples or radix keys have already
been computed. This reports direct array/dictionary access as well as the
public
lookup_keyAPI. It excludes tokenization and key construction. - Prepared key-batch scoring: all radix keys are available before timing. This is useful when an application updates or reuses encoded windows.
Numeric results are checked before timing. Score values are negative binary fractions so that float32 accumulation does not obscure correctness with large rounding differences. The exact observed-key fraction of each candidate is reported; the nominal observed fraction is 50% of unique prepared windows. The sparse model contains only these observed keys and returns the same default score as the dense model for all other keys.
speedup_vs_legacy is legacy time / new time. Above 1 means the new
implementation was faster; below 1 means it was slower. Scalar lookup ratios
use the existing direct NumPy tensor access as their reference. Do not mix
scalar lookup ratios with whole-text scoring ratios: they measure different
amounts of work.
A fixed-width array address is constant-time once a key exists. A sparse Python hash-table lookup has expected constant-time table access, but key encoding and hashing of arbitrary-size integers depend on their size. For arbitrary n or very large vocabularies, identical latency to a small dense character model cannot be guaranteed.
The new formats are decoded only when loading. Parsing, compression and checksums are not part of these in-memory hot-loop measurements.