Skip to content

Performance and the native build

microbook is pure Python with no runtime dependencies. This page gives the measured speed, shows how to measure it on your machine, and explains the optional compiled build of the matching engine.

How fast it is

benchmarks/bench_book.py sends 200,000 messages through one book: 55% passive limit orders, 35% cancels and 10% aggressive orders, roughly the mix of real order traffic, where most messages never trade. On an Apple M2 with CPython 3.12:

v0.1 1.0
Throughput 139k msg/s 491k msg/s
Cancel p50 / p99 13.8 µs / 44.4 µs 0.50 µs / 1.00 µs
Aggressive p50 2.00 µs 1.96 µs
Passive add p50 0.54 µs 0.88 µs

Passive adds got slower in v0.2 because they now pay for duplicate-id checks, tick validation and an execution report. Cancels dominate real traffic, so throughput still rose 3.5×. The full simulator, with background agents, a strategy and analytics, runs at about 200,000 events a second (benchmarks/bench_sim.py).

Measure your own machine from a checkout:

python benchmarks/bench_book.py                 # the engine
python benchmarks/bench_sim.py                  # the whole simulator
python benchmarks/profile_book.py               # a cProfile breakdown of the engine

The compiled engine

The engine (book.py, order.py and types.py) can be compiled with mypyc, which turns type-annotated Python into a C extension. It is off by default and needs a C compiler. To install the compiled build from PyPI:

HATCH_BUILD_HOOK_ENABLE_MYPYC=true pip install --no-binary microstructure-sim microstructure-sim

From a checkout, build through the sdist, which compiles in a temporary copy:

pip install build
HATCH_BUILD_HOOK_ENABLE_MYPYC=true python -m build -o native/
pip install native/*.whl

Do not compile in place

Avoid pip install . and python -m build --wheel with the hook enabled. mypyc then compiles inside src/microbook/ and leaves .so files there. Those shadow the Python sources, so an editable install silently ignores your edits until you delete them.

To check which build you are running:

import microbook.book

print("compiled" if not microbook.book.__file__.endswith(".py") else "pure Python")

What changes

The code is the same, and CI runs the whole test suite against the compiled wheel on the oldest and newest supported Python, including the golden digests of seeded runs. Results are identical with or without it.

One visible difference: mypyc drops class docstrings, so help(LimitOrderBook) is empty in a compiled install. This site has them.

Best of 3 runs of the engine benchmark, both at 1.0 on the same Apple M2:

pure Python compiled
Throughput 491k msg/s 591k msg/s
Passive add p50 0.88 µs 0.58 µs
Aggressive p50 1.96 µs 1.46 µs
Cancel p50 0.50 µs 0.38 µs

The whole simulator gains only 10–20%, because the agents and strategies stay interpreted: they are meant to be subclassed. That gain is too small to justify platform-specific wheels, so the published wheel is pure Python and the native build is opt-in.

Making a run faster

Most of a simulation's time goes to the agents and the strategies, not the engine. In rough order of effect:

  • Sample less often. Raise MarketConfig.snapshot_every: every SNAPSHOT calls every strategy's on_tick and samples PnL. Keep record_snapshots off unless you need the L2 snapshots.
  • Run seeds in parallel. Runs are independent and deterministic, so a ProcessPoolExecutor over seeds scales with cores; examples/compare_market_makers.py does this.
  • Keep hooks cheap. A strategy that does work on every tick, rather than on a timer every n steps, costs time on every snapshot.
  • Compile the engine, as above, for the last 10–20%.