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 benchmarktitle(str): Display name for the algorithmsetup_fn(Callable, optional): Function called before timing to prepare test data
REPEAT(int, default=1000): Number of times to run each algorithmverbose(bool, default=True): Whether to print progress and results
Returns:
- List[Dict]: Results for each algorithm containing:
title: Algorithm namesetup_time: Time spent in setuptotal_time: Total execution timeavg_time: Average time per iterationlast_result: Result from the last iterationtotal_perf: Combined setup + execution timeerror: 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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file simple_function_benchmark-0.1.2.tar.gz.
File metadata
- Download URL: simple_function_benchmark-0.1.2.tar.gz
- Upload date:
- Size: 3.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f961f2f923591f87d4047d90e08fe55ded208eba05ae9176bc25859c6f1f3bda
|
|
| MD5 |
0abd348d12c10108ee787bdd2d38e494
|
|
| BLAKE2b-256 |
dc43d85647d35867e749e4a0922b843495758e04cb3c46d7e35ea8c7c3043cb8
|
File details
Details for the file simple_function_benchmark-0.1.2-py3-none-any.whl.
File metadata
- Download URL: simple_function_benchmark-0.1.2-py3-none-any.whl
- Upload date:
- Size: 4.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1e5d63cc86b4105f316491dd066ae80ebb7905aa17e978394d40648df7ca1f1c
|
|
| MD5 |
4fc5a13db12fa9cfef4bf2ee86f3dbf7
|
|
| BLAKE2b-256 |
6f823355e62059d9af1167f4b09b805bb0c8a9728692382a2f2a1fbebc5027e8
|