Benchmark · C++17
An order book that matches 59 million messages a second
A limit order book pairs buy and sell orders by price, then by arrival time. Two C++17 versions run the same seeded order flow: MapBook keeps price levels in a std::map; FlatBook keeps levels near the market in a flat array indexed by price.
Synthetic order flowSingle runOne threadApple M5 Pro
- CPU
- Apple M5 Pro
- Compiler
- Apple LLVM 21.0.0 (clang-2100.4.1.1)
- Flags
- -O3 -DNDEBUG -std=c++17
- Runs
- 1 (no spread available)
How a book matches
An order trades when it reaches the best price on the other side
Step through 200 orders. Buyers wait on the left, sellers on the right; a trade happens when an order reaches the other side.
- LimitTrades what it can at its price or better, then rests the remainder.
- IOCImmediate or cancel: trades what it can up to its price, cancels the rest.
- FOKFill or kill: trades its full size at once or does nothing.
- Post-onlyRests only if it wouldn't trade on arrival.
- MarketIgnores its price and takes the best prices available.
Cancel removes a resting order. Modify keeps the order's place in the queue if it only shrinks; a price change or a larger size sends it to the back.
Operation
Result
Last 5 operations
Keys when the demo has focus: Space plays or pauses, ← → step, Home resets.
Replayed through MapBook. The order script is synthetic (examples/demo_flow.txt). The differential test checks that FlatBook produces the same fills on generated flows. Dashed rows traded in the current step.
Where the difference comes from
FlatBook finds most price levels by index instead of walking a tree
Most messages touch prices near the best bid and ask. FlatBook keeps those levels in a contiguous array, pools its orders so resting orders don't allocate after warm-up, and caches the best price on each side.
Red-black tree of levels
Finding a level follows about log₂(n) pointers, and each one can miss the cache. Each level's orders sit in a linked list.
Array indexed by price tick
The level for price p is slot p − base, and neighbouring levels share cache lines. Prices outside the window fall back to a std::map.
Design, not a measurement. The benchmark times whole books, so it doesn't show how much each of the three differences contributes.
Per-message latency
Per-message latency sits at the timer's resolution, so it can't separate the books
FlatBook's p99 is one tick lower on resting adds, crossing adds and market orders, and equal on cancels. Every difference is under two 41 ns clock ticks, within the timer's resolution.
Latency percentiles by operation
Apple M5 Pro · 1,000,000 messages per book · single run
A value of 0 ns means the message finished before the clock advanced, not that it took no time. Anything under one tick reads as 0 or one tick.
Methodology
How this was measured
The benchmark generates one seeded stream of 1,100,000 messages (seed 20240611): 55% add, 40% cancel, 5% market. 10% of adds are priced through the best price, so they trade on arrival. The first 100,000 messages warm the book and are not measured.
Throughput is the measured messages divided by the time to process them, with warm-up excluded from timing and averaged over Google Benchmark's repeated passes (items per second). Latency is one steady_clock sample around each message, grouped by operation type afterwards: an add that produced fills counts as crossing.
Each book ran once, so there is no run-to-run spread. The machine's load average was 11–13 on 15 CPUs during the run, so other work may have added noise.
- Timer resolution
- lob_bench estimates one tick at 41 ns; the recorded values are multiples of about 41.6 ns.
- p50, p99, p99.9
- The latency that 50%, 99% or 99.9% of messages beat.
- Resting / crossing add
- An add that rests in the book, or one that trades on arrival.
What this shows
- FlatBook sustains 1.89× MapBook's throughput on this flow, on one thread.
- Both books produce the same fills as a naive reference, checked op by op.
- Per-message p99 latency is equal within two clock ticks.
What it doesn't show
- Performance on real market data: the flow is synthetic.
- Run-to-run spread: each book was measured once.
- Latency differences smaller than two clock ticks.
- Multiple instruments, threads, networking or a market data feed.
Reproduce
Run it yourself
git clone https://github.com/allenwang0/limitorderbook.git
cd limitorderbook
cmake --workflow --preset default # Release build + tests
./build/lob_bench # latency and throughput benchmark
./build/lob_replay examples/demo_flow.txt
scripts/run_bench.sh # rerun everything and rebuild this pageNeeds CMake 3.25+, Ninja and a C++17 compiler. This page is built from the committed results/bench.json; CI reruns a shorter benchmark on every push but keeps it as a build artifact only.
Full environment · 11 rows
| Field | Value |
|---|---|
| CPU | Apple M5 Pro (15 cores) |
| Compiler | Apple LLVM 21.0.0 (clang-2100.4.1.1) |
| Flags | -O3 -DNDEBUG -std=c++17 |
| Google Benchmark | v1.9.1 |
| Commit recorded by the run | 346f4de |
| Run date | 2026-11-11 |
| Clock tick | 41 ns |
| Load average | 11.2, 13.4, 11.4 |
| Repetitions | 1 |
| Seed | 20240611 |
| Data file | results/bench.json |