Skip to main content

⚡ DsaBench

One-line benchmarking for DSA, competitive programming, and interview prep.

CI PyPI Python License: Apache 2.0 Code style: black Typed

You wrote two solutions to a problem. Which one is faster? How much memory does the recursive one eat? How deep does it recurse? Is your "optimised" version actually O(n log n)?

dsabench answers all of that with one line — no boilerplate, no timeit incantations, no manual tracemalloc bookkeeping:

from bench import bench

answer = bench(solve, arr, target)   # runs it, prints the report below,
print(answer)                        # ...and still hands back the real answer
╭─ ⚡ Benchmark Report ──────────────────────────────────╮
│                                                        │
│  Function               solve                          │
│  Arguments              ([3, 1, 2], 4)                 │
│  Mode                   default · 10 runs (+1 warmup)  │
│  Return                 [1, 3]                         │
│  Time                                                  │
│    Fastest              19.293 µs                      │
│    Average              22.169 µs                      │
│    Median               19.457 µs                      │
│    Slowest              46.048 µs                      │
│    Std Dev              8.393 µs                       │
│    95th pct             34.303 µs                      │
│  Memory                                                │
│    Peak                 2.66 KB                        │
│    Current              448 B                          │
│    Delta                448 B                          │
│    Process RSS          24.95 MB                       │
│  CPU                                                   │
│    CPU time             22.680 µs                      │
│    CPU %                102.3%                         │
│  Calls                                                 │
│    Function calls       465                            │
│    Recursive calls      464                            │
│    Max recursion depth  12                             │
│    GC collections       0                              │
│                                                        │
╰────────────────────────────────── 2026-07-08T16:53:33 ─╯

Install

pip install dsabench            # core (only dependency: rich)
pip install "dsabench[viz]"     # + matplotlib graphs

Python 3.10+. Fully typed (py.typed), Apache-2.0 licensed.

Three ways to use it

1 · Function call — bench()

from bench import bench

def fib(n):
    return n if n < 2 else fib(n - 1) + fib(n - 2)

value = bench(fib, 20)                      # full report, returns 6765
value = bench(fib, 20, mode="accurate")     # 100 runs + 5 warmups
value = bench(fib, 20, repeat=50, warmup=2) # explicit control
value = bench(fib, 20, quiet=True)          # measure silently

bench() is transparent: whatever the function returns, you get back — including for generator functions (you receive a fresh generator) and coroutine functions (awaited for you when no event loop is running).

2 · Decorator — @benchmark

from bench import benchmark

@benchmark                       # bare form
def solve(nums):
    return sorted(nums)

@benchmark(mode="accurate")      # parameterised form
def solve_fast(nums):
    return sorted(nums)

solve([3, 1, 2])                 # report prints on every call
solve.last_result.average_ms     # latest BenchmarkResult, programmatically
solve.original([3, 1, 2])        # raw, un-instrumented function

Recursive and nested decorated functions are handled correctly: inner calls run the original function, so you get exactly one report per outermost call and true recursion statistics (a @benchmark-ed fib(10) reports 177 calls at depth 10, not a storm of nested reports).

3 · Automatic — auto()

from bench import auto, stop_auto

auto()                # from here on, every user-defined function you call
                      # is timed and printed live — stdlib & site-packages
                      # are ignored automatically

run_my_pipeline()

stop_auto()           # prints a ranked summary table (calls, total, avg, min, max)

Filter what gets captured with fnmatch patterns: auto(include=["solve*"]) or auto(exclude=["helper_*"]).

Compare solutions head-to-head

from bench import compare

result = compare(
    ("Memoization", fib_memo),
    ("Tabulation",  fib_tab),
    ("Matrix power", fib_matrix),
    args=(30,),
)
result.winner.name        # "Matrix power"
⚡ Comparison — args (30)
╭──────┬──────────────┬──────────┬──────────┬──────────┬──────────┬──────────────╮
│ Rank │ Name         │  Average │  Fastest │  Std Dev │ Peak Mem │     Relative │
├──────┼──────────────┼──────────┼──────────┼──────────┼──────────┼──────────────┤
│    1 │ Matrix power │ 11.9 µs  │ 10.8 µs  │ 1.1 µs   │  1.2 KB  │     baseline │
│    2 │ Memoization  │ 38.4 µs  │ 35.0 µs  │ 2.9 µs   │  9.8 KB  │ 3.22× slower │
│    3 │ Tabulation   │ 51.7 µs  │ 48.2 µs  │ 3.4 µs   │  2.1 KB  │ 4.34× slower │
╰──────┴──────────────┴──────────┴──────────┴──────────┴──────────┴──────────────╯
✓ all outputs match

compare also verifies that all candidates return equal outputs — the fastest wrong answer is still wrong.

Estimate Big-O empirically

from bench import estimate_complexity

estimate_complexity(my_sort, sizes=[1_000, 2_000, 4_000, 8_000, 16_000],
                    args_for=lambda n: (random_list(n),))
Estimated complexity: O(n log n)   (R² = 0.998)

Fits your measured times against O(1), O(log log n), O(log n), O(n), O(n log n), O(n²), O(n³), O(2ⁿ), O(3ⁿ), O(eⁿ), and O(n!) by least squares and ranks them by R². Treat it as a sanity check, not a proof — constant factors and caches are real.

Need a shape that's not on the list — O(n⁴log²n), a variable exponent, anything? Compose your own from bench.complexity's builders and pass models=:

from bench.complexity import COMPLEXITY_MODELS, polylog

custom = COMPLEXITY_MODELS + [("O(n⁴log²n)", polylog(4, 2))]
estimate_complexity(my_func, sizes=[...], models=custom)

Estimate space complexity empirically

Same fitting machinery, but for peak memory instead of time — useful for answering "is my DP table actually O(n²) in space, or did I accidentally make it O(n³)?":

from bench import estimate_space_complexity

estimate_space_complexity(build_dp_table, sizes=[50, 100, 200, 400, 800],
                          args_for=lambda n: (n,))
Estimated space complexity: O(n²)   (R² = 0.9999)

Takes the same models= override and builder DSL as estimate_complexity.

Export and plot

from bench import run, export_result, plot_runtime

result = run(fib, args=(25,))                # like bench(), but returns the
                                             # BenchmarkResult and never prints
export_result(result, "fib.json")            # .json / .csv / .md by extension
plot_runtime(result, path="fib.png")         # needs dsabench[viz]

Or export everything automatically:

import bench
bench.configure(export="json", export_dir="benchmarks/")

Configuration

import bench
bench.configure(repeat=100, warmup=5, color=True, memory=True,
                cpu=True, export="json")
Option Default Meaning
mode "default" fast (1 run) / default (10+1) / accurate (100+5)
repeat mode Timed repetitions (overrides mode)
warmup mode Untimed warmup runs (overrides mode)
color True Colored Rich output
memory True Track memory via tracemalloc
cpu True Track CPU time / CPU %
profile True Track calls / recursion / GC
quiet False Suppress printed reports
raise_exceptions True Re-raise exceptions from benchmarked code
export None Auto-export every result: "json"/"csv"/"md"
export_dir "." Where auto-exports are written
precision 3 Decimal places in time formatting
show_args True Include the arguments line in reports

reset_config() restores the defaults; get_config() returns the active configuration.

How measurement works (and why you can trust it)

  • Warmups first (never timed) — caches warm, imports settled.
  • Clean timed runs using time.perf_counter_ns (wall) and time.process_time_ns (CPU). Nothing else happens inside the timed region — no memory tracking, no call counting, no allocation by dsabench itself.
  • One separate instrumented pass afterwards collects memory (tracemalloc), call counts, recursion depth, and GC activity, so the instrumentation overhead never pollutes your timings.

That means the default mode calls your function 12 times: 1 warmup + 10 timed + 1 instrumented. In fast mode it's a single call with everything combined (numbers are slightly noisier — that's the trade-off).

Other honest details:

  • CPU % can exceed 100% for multi-threaded code — process_time_ns sums all threads. That's signal, not a bug.
  • Call counting uses sys.setprofile, counts Python-level frames (stdlib included, cProfile-style), and excludes dsabench's own machinery. A recursion like fib(10) reports exactly 177 calls, 176 recursive, depth 10.
  • For coroutine functions, call/recursion tracking is disabled (profile hooks and event loops don't mix reliably); timing and memory still work.
  • tracemalloc sees Python allocations. C-extension allocations (NumPy buffers, etc.) show up in Process RSS, not in peak/delta.

Jupyter & async

Inside a running event loop (Jupyter, async apps), bench() on a coroutine can't call asyncio.run — use the awaitable form:

value = await bench_async(fetch_data, url)

The @benchmark decorator works on async def functions out of the box.

FAQ

Why is the import called bench when the package is dsabench? pip install dsabench, from bench import bench — short at the call site, unique on PyPI. Heads-up: the Frappe/ERPNext ecosystem ships a CLI tool also distributed as bench on PyPI. Don't install that package into the same environment as dsabench: both would own the bench import name and clash.

Do warmup runs affect my numbers? No — they're executed and discarded before the timing loop.

Why do I see 12 executions in default mode? 1 warmup + 10 timed + 1 instrumented pass. Functions with side effects (appending to a list, writing files) will apply them each time — benchmark pure functions, or pass fresh inputs.

Can it benchmark code that raises? Yes. The exception is captured, shown in the report, and re-raised by default (configure(raise_exceptions=False) to suppress). run() never raises target exceptions; check result.exception.

Is auto() production-safe? It installs a sys.setprofile hook, which slows everything down while active. It's a development and learning tool — call stop_auto() when done.

Timer resolution? perf_counter_ns has nanosecond units; actual resolution is platform-dependent (typically tens of ns). For micro-functions, use mode="accurate" and read the median.

Roadmap

  • bench.timeline() — flame-graph style per-call timeline for auto()
  • Statistical significance testing in compare() (Mann–Whitney U)
  • --bench pytest plugin for inline regression benchmarks
  • HTML report export
  • Per-line memory attribution (top allocating lines from tracemalloc)

Contributing

PRs welcome — see CONTRIBUTING.md. Run ruff check . && black --check . && pytest before pushing; all three are enforced by CI on Python 3.10–3.13.

License

Apache-2.0 © 2026 Priyadip

Metadata

Release files for dsabench 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for dsabench 0.3.0
File Size Uploaded Provenance Actions
dsabench-0.3.0.tar.gz 55.4 kB With provenance Details for dsabench-0.3.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for dsabench 0.3.0
File Interpreter ABI Platform Actions
dsabench-0.3.0-py3-none-any.whl Python 3 none any Details for dsabench-0.3.0-py3-none-any.whl

Total release size: 103.4 kB

Release files / dsabench-0.3.0.tar.gz

Download URL dsabench-0.3.0.tar.gz
Size 55.4 kB
Tags Source
SHA-256 checksum
How to use checksums
cb68edb3859b1f32523ec70c142562f0f01c9a03ef4b920f5af850514a03f896
BLAKE2b-256 checksum
How to use checksums
486b1b69cbda133c29f791cccb9498227a9db4ee74f83093d8df6dbeab00fab9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 9, 2026.

Transparency log

Release files / dsabench-0.3.0-py3-none-any.whl

Download URL dsabench-0.3.0-py3-none-any.whl
Size 48.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
dcdbe89413c2b894f53de6680f5a2b757c4e81233fa9baab38646bfc54e22409
BLAKE2b-256 checksum
How to use checksums
b7030a4300cf215d0c4466588f086c9549731badd6b95b2563db6c022d4ff0b3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 9, 2026.

Transparency log

Security for dsabench 0.3.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for dsabench-0.3.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 9, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page