Skip to main content

Simple benchmarking library for comparing algorithm runtime

Project description

Benchmark

A simple, easy-to-use Python benchmarking library for comparing algorithm performance.

Features

  • 🚀 Simple API - Compare multiple algorithms with just a few lines of code
  • 📊 Detailed Results - Get setup time, total time, average time, and performance comparisons
  • 🔄 Progress Tracking - Real-time progress updates during long-running benchmarks
  • 🛡️ Error Handling - Gracefully handles algorithm failures without stopping the entire benchmark
  • 📈 Performance Ratios - Automatically shows how much slower each algorithm is compared to the best

Installation

pip install benchmark

Quick Start

import benchmark

# Define your algorithms to compare
algorithms = [
    {
        "title": "Bubble Sort",
        "algorithm_fn": bubble_sort,
        "setup_fn": lambda: [3, 1, 4, 1, 5, 9, 2, 6]
    },
    {
        "title": "Python's sorted()",
        "algorithm_fn": sorted,
        "setup_fn": lambda: [3, 1, 4, 1, 5, 9, 2, 6]
    }
]

# Run the benchmark
results = benchmark.run(algorithms, REPEAT=1000)

Usage

Basic Example

import benchmark

def algorithm1(data):
    return sorted(data)

def algorithm2(data):
    return list(reversed(sorted(data, reverse=True)))

algorithms = [
    {
        "title": "Standard sort",
        "algorithm_fn": algorithm1,
        "setup_fn": lambda: [5, 2, 8, 1, 9]
    },
    {
        "title": "Reverse then reverse",
        "algorithm_fn": algorithm2,
        "setup_fn": lambda: [5, 2, 8, 1, 9]
    }
]

results = benchmark.run(algorithms, REPEAT=10000, verbose=True)

Output Example

[1/2] Running: Standard sort... Done (0.05s)
[2/2] Running: Reverse then reverse... Done (0.08s)

Benchmark Results:
Standard sort                       setup: 0.0000s  total: 0.0500s  avg: 5.00us <-- BEST
Reverse then reverse                setup: 0.0000s  total: 0.0800s  avg: 8.00us (1.60x slower)

API Reference

benchmark.run(algorithms, REPEAT=1000, verbose=True)

Run a benchmark comparing multiple algorithms.

Parameters:

  • algorithms (List[Dict]): List of algorithm dictionaries with keys:
    • algorithm_fn (Callable): The function to benchmark
    • title (str): Display name for the algorithm
    • setup_fn (Callable, optional): Function called before timing to prepare test data
  • REPEAT (int, default=1000): Number of times to run each algorithm
  • verbose (bool, default=True): Whether to print progress and results

Returns:

  • List[Dict]: Results for each algorithm containing:
    • title: Algorithm name
    • setup_time: Time spent in setup
    • total_time: Total execution time
    • avg_time: Average time per iteration
    • last_result: Result from the last iteration
    • total_perf: Combined setup + execution time
    • error: Error message if the algorithm failed, None otherwise

Examples

See the included demo in benchmark.py which compares sorting algorithms:

  • Bubble Sort
  • Timsort (Python's built-in sorted())
  • Heap Sort
  • Quicksort

Requirements

  • Python >= 3.7

License

MIT License

Author

ControlAltPete (peter@petertheobald.com)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

simple_function_benchmark-0.1.1.tar.gz (2.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

simple_function_benchmark-0.1.1-py3-none-any.whl (2.5 kB view details)

Uploaded Python 3

File details

Details for the file simple_function_benchmark-0.1.1.tar.gz.

File metadata

  • Download URL: simple_function_benchmark-0.1.1.tar.gz
  • Upload date:
  • Size: 2.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.10 {"installer":{"name":"uv","version":"0.9.10"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for simple_function_benchmark-0.1.1.tar.gz
Algorithm Hash digest
SHA256 d52439036ec79b91491b32a75ae304d8181ec1758053660b9996b2de73d10dcb
MD5 a4576a9ea9433acdb52f4cb822995800
BLAKE2b-256 14f620bb1be2c295ad2f499c5986953ede73185b4a7f220e89f0e049c3c5209f

See more details on using hashes here.

File details

Details for the file simple_function_benchmark-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: simple_function_benchmark-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 2.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.10 {"installer":{"name":"uv","version":"0.9.10"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for simple_function_benchmark-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 188638dacba420c4411505c07c7629ad77a0f9fabd13be1d8d664f2f9129f834
MD5 1e7994ae4aaa79ed339950f87a851ea8
BLAKE2b-256 49a4205fae1fd750f7933d378bbd282cd9e122003a89a8e102f3c05afd1a34ae

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page