benchmark
Implements the benchmark module for runtime benchmarking.
You can import these APIs from the benchmark
package. For example:
import benchmarkfrom time import sleep
You can pass any fn
as a parameter into benchmark.run[...]()
, it will return a Report
where you can get the mean, duration, max, and more:
fn sleeper(): sleep(.01)var report = benchmark.run[sleeper]()print(report.mean())
0.012256487394957985
You can print a full report:
report.print()
---------------------Benchmark Report (s)---------------------Mean: 0.012265747899159664Total: 1.459624Iters: 119Warmup Mean: 0.01251Warmup Total: 0.025020000000000001Warmup Iters: 2Fastest Mean: 0.0121578Slowest Mean: 0.012321428571428572
Or all the batch runs:
report.print_full()
---------------------Benchmark Report (s)---------------------Mean: 0.012368649122807017Total: 1.410026Iters: 114Warmup Mean: 0.0116705Warmup Total: 0.023341000000000001Warmup Iters: 2Fastest Mean: 0.012295586956521738Slowest Mean: 0.012508099999999999Batch: 1Iterations: 20Mean: 0.012508099999999999Duration: 0.250162Batch: 2Iterations: 46Mean: 0.012295586956521738Duration: 0.56559700000000002Batch: 3Iterations: 48Mean: 0.012380562499999999Duration: 0.59426699999999999
If you want to use a different time unit you can bring in the Unit and pass it in as an argument:
from benchmark import Unitreport.print(Unit.ms)
---------------------Benchmark Report (ms)---------------------Mean: 0.012312411764705882Total: 1.465177Iters: 119Warmup Mean: 0.012505499999999999Warmup Total: 0.025010999999999999Warmup Iters: 2Fastest Mean: 0.012015649999999999Slowest Mean: 0.012421204081632654
The unit’s are just aliases for StringLiteral
, so you can for example:
print(report.mean("ms"))
<