Benchmark class¶
Provide micro benchmarking functionality
- class benchmark.Benchmark(name: str | None = None, runs: int = 1000)¶
Create and run a microbenchmark.
Use this class as a context manager. Create the
Benchmarkand set the test subject withset_user_code(). Rayne automatically measures and prints the test subject’s execution time when the context manager exits.with Benchmark() as benchmark: benchmark.set_user_code(fibonacci, n=10)
- name¶
Rayne assigns a name to each
Benchmark. Rayne uses the name in the output to differentiate multiple benchmarks within a single benchmarking script. The default name is the string representation of the test subject.>>> with Benchmark() as benchmark: >>> benchmark.set_user_code(fibonacci, n=10) >>> print(benchmark.name) fibonacci
You can assign a custom name to the
Benchmark.>>> with Benchmark(name="Recursive") as benchmark: >>> benchmark.set_user_code(fibonacci, n=10) >>> print(benchmark.name) Recursive
>>> with Benchmark() as benchmark: >>> benchmark.name = "Recursive" >>> benchmark.set_user_code(fibonacci, n=10) >>> print(benchmark.name) Recursive
- runs¶
Rayne runs the test subject several times to build a statistical model of the execution time. The
runsattribute controls how many times Rayne runs the test subject. You can modify this value before theBenchmarkcontext manager exits. For example, to run the test subject 2000 times:with Benchmark(runs=2000) as benchmark: benchmark.subject = fibonacci
with Benchmark() as benchmark: benchmark.runs = 2000 benchmark.subject = fibonacci
- property run_time: float¶
The average execution time, in nanoseconds, of the subject function.
- set_user_code(function, **kwargs)¶
Set the user code to benchmark.
- Parameters:
function (Callable) – The benchmark measures the execution time of this function.
kwargs – The benchmark passes these arguments to
function.
- class benchmark.BenchmarkResults(name: str, run_times: List[int])¶
Benchmark name and associated run times.