Introduction

Simple microbenchmarking of Python snippets, powered by Google Benchmark.

mew is a small Python library and CLI for writing microbenchmarks the way you write tests: decorate a function, run mew, get reliable timings, backed by Google Benchmark and built with nanobind.

import mew


@mew.benchmark
def bench_sorted(state: mew.State) -> None:
    data = list(range(1000, 0, -1))
    for _ in state:
        sorted(data)
$ mew run
mew · host=laptop cpus=10 scaling=enabled
Benchmark                              │       Iters │           Real │            CPU
─────────────────────────────────────────────────────────────────────────────────────
benchmarks/bench_sort.py::bench_sorted │   1,000,000 │      32.10 ns │      32.05 ns

At a glance

Decorate

Register one benchmark or a family with @mew.benchmark, @mew.parametrize, or @mew.product.

Writing benchmarks
Run

mew run discovers bench_*.py files, runs them with Google Benchmark, and streams results to the terminal, JSON, or a JSONL archive.

Command-line interface
Profile

--sample for pyinstrument CPU sampling, --profile-memory for memray allocations. For native C frames, sample the process from outside (see the native-profiling recipe).

CPU profiling
Compare

mew compare head.json baseline.json --regression-threshold 5% --exit-non-zero-on-regression for CI regression gates with an allowlist.

Comparisons and regression gating

Table of Contents

Getting started