Skip to content

Performance

zeki is designed for speed first: a small memory footprint, and full use of every core of a modern processor.

How it's fast

A lazy expression graph. Building a model computes nothing: each collection records how its values are computed. When you collect, zeki evaluates only what the collected values depend on, so work whose results are never used is never done.

Chunks in cache-sized buffers. Currently available commercial capital modelling products, NumPy and Polars compute each step over every simulation and keep the result, so a model of many steps holds many full-size intermediate arrays. zeki instead runs the whole model over a chunk of simulations at a time, in small reused buffers sized to stay in the processor's L2 cache, and writes only the values you collect. Intermediate values are never held in full, so models run on cheaper hardware.

Every core, without the GIL. Chunks are evaluated in parallel across every core, and the GIL is released while they are, so other Python threads keep running.

A compiled, vectorised engine. The engine is written in Rust, its loops compiled to SIMD instructions, with its own vectorised exp, log and pow (see Maths functions). On x86-64, a build for processors with AVX2 and FMA is chosen automatically where the processor supports it.

Copulas in the right place. Because evaluation is deferred, zeki can apply a copula to the inputs before anything is computed from them. Values built from those inputs are then computed in the reordered order to begin with, rather than computed and reordered afterwards.

Dense jagged values. A jagged value holds every value in one array, with a second array of counts, so a low-frequency distribution costs memory only for the events that happen.

Writing fast models

  • Collect once. Each collect() is one engine run. Gather what you need and collect it together with zk.collect_all, so shared work is done once.
  • Register copulas before collecting. A copula registered after a collect makes the earlier values out of date, and collecting again recomputes them.
  • Prefer collection operations to per-row Python. zk.float and add call Python once per row, which is fine for building inputs. Arithmetic, with_columns and group-bys run in the engine.

The result cache

zeki keys every value by a hash of its inputs' data and the operations that combine them, and caches evaluated values and copula reorderings by that key. Re-running a model, or collecting a value that shares work with an earlier one, reuses what's already computed. The cache never changes a result.

Function Does
zk.cache_info() The cache's entries, bytes, limit, hits and misses
zk.set_cache_limit(bytes) Sets the limit, 1 GiB by default. 0 turns the cache off.
zk.clear_cache() Empties the cache and resets its counters

Threads and timings

zk.num_threads() is the number of threads zeki evaluates on, one per logical processor by default. zk.set_num_threads(n) changes it, if called before the first evaluation.

zk.timings() gives the seconds zeki has spent on each of its activities, such as evaluating, and zk.timing_counts() how many times each started. zk.reset_timings() sets both back to zero.

Benchmark

A randomly generated program of 50,000 operations (+ - * /, min and max, abs, sqrt, negation) over 15,000 inputs of 50,000 values (6 GB), with 25 outputs. Each operation reads an input or one of the 32 results just before it, as a large model's formulas mostly read recent intermediate values. Each path runs the same program on the same inputs, and its outputs agree with zeki's to within 6.4e-16 relative.

Path How it runs the program
zeki zeki's engine, built from Python with zk.engine, evaluated in one pass
Julia A hand-written interpreter for this program: each task runs the whole program over a chunk of simulations, the best of a sweep of chunk sizes
Julia GPU One CUDA kernel: a thread per simulation runs every operation
Rust The same hand-written interpreter as the Julia path, in Rust with rayon
PAL The Proteus Actuarial Library: one eager whole-array NumPy operation per program operation
PAL GPU The same, on PAL's CuPy backend

Measured on an Intel i9-12900K (8 performance and 8 efficiency cores, 24 threads), with 64 GB of DDR5-5600 and an RTX 4060.

Evaluation: seconds per evaluation once everything is built and the inputs are in place, the cost of re-running a model.

Path Parallel or GPU Single-threaded
zeki 0.21 1.39
Julia 0.17 1.17
Julia GPU 0.18 -
Rust 0.18 1.14
PAL - 4.98
PAL GPU 4.98 -

End to end: seconds from opening the input file to the 25 outputs in memory, run once. Load inputs reads the inputs into wherever the path evaluates them, build makes the program runnable, and first eval includes Julia's compilation on first call.

Path Load inputs Build First eval Total
zeki 3.44 0.13 0.20 3.77
Julia 1.37 0.00 0.18 1.55
Julia GPU 1.97 0.21 2.16 4.35
Rust 1.40 0.00 0.21 1.61
PAL 1.39 0.01 4.99 6.39
PAL GPU 2.52 0.01 5.10 7.64
  • zeki is within 20% of interpreters hand-written for this one program. All three CPU paths are close to the limit of the machine's memory bandwidth: each evaluation reads 11.75 GB of inputs, and 0.17 to 0.18 seconds is 65 to 70 GB/s.
  • End to end, loading is zeki's gap. Building and compiling the 50,000 operations from Python takes 0.13 seconds, but zeki copies the 15,000 inputs in one at a time, where the interpreters read the whole file into one array in a single call.
  • The GPU is no faster here. Each simulation's 50,000 operations form one dependent chain, so the GPU kernel is bound by memory latency rather than bandwidth.
  • PAL is 24 times slower than zeki, and its GPU backend doesn't help. PAL runs each operation eagerly over the whole array, on one thread, and tracks the coupling between variables on every operation, which is two-thirds of its time. On the GPU, each operation is a separate launch over 50,000 values, so the same per-operation overhead dominates. The PAL path is already tuned within PAL's API: its inputs are held as arrays and wrapped only where they're read, and each result is written into a reused buffer, which together made it about 27 times faster than a direct port.