← Allen Wang Code ↗

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

1.89×FlatBook's throughput relative to MapBook on the same 1,000,000 messages.
Per-message latency, p99Within one tickThe books' p99s differ by at most one tick (41 ns) per operation.p99 FlatBook 42–84 ns · MapBook 42–125 ns · 4 operation types
Correctness20 seeds× 100,000 ops each. Both books are checked op by op against a naive reference book. The same test also runs under ASan and UBSan.tests/differential_test.cpp
Workload1 M messages55% add, 40% cancel, 5% market, after 100,000 warm-up messages.Seed 20240611 · synthetic, not market data
Conditions
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)
Full environment →

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.

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.

      MapBook

    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.

    1004100210061001100310051007
      FlatBook

    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.

    1001100210031004 slot = price − 1001one contiguous block

    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

    Each row is one operation type, one lane per book. The line runs from p50 to p99.9, the filled dot marks p50 and the ring marks p99. Vertical gridlines fall on clock ticks. Single run of 1,000,000 messages per book, so no run-to-run spread. The table adds p90, max and sample counts.

    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 page

    Needs 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
    FieldValue
    CPUApple M5 Pro (15 cores)
    CompilerApple LLVM 21.0.0 (clang-2100.4.1.1)
    Flags-O3 -DNDEBUG -std=c++17
    Google Benchmarkv1.9.1
    Commit recorded by the run346f4de
    Run date2026-11-11
    Clock tick41 ns
    Load average11.2, 13.4, 11.4
    Repetitions1
    Seed20240611
    Data fileresults/bench.json