Writing benchmarks¶
Basics¶
Writing a benchmark is as simple as extenting Benchmark
.
Each method will be run times
times.
A benchmark method should start with bench_
.
from minibench import Benchmark
class SumBenchmark(Benchmark):
times = 1000
def bench_sum(self):
sum(x for x in range(5))
def bench_consecutive_add(self):
total = 0
for x in range(5):
total += x
$ bench examples/sum.bench.py
Running 1 benchmark
-------------------
>>> Sum benchmark (x1000)
Consecutive add................... ✔ 0.00142s / 0.00000s
Sum............................... ✔ 0.00245s / 0.00000s
✔ Done
Documenting¶
Documenting you benchmark is as simple as writing explicit docstrings. Only the first line will be kept.
from minibench import Benchmark
class SumBenchmark(Benchmark):
'''
A simple sum benchmark
This will be ignored.
'''
times = 1000
def bench_sum(self):
'''Sum using sum()'''
sum(x for x in range(5))
def bench_consecutive_add(self):
'''Sum using +='''
total = 0
for x in range(5):
total += x
$ bench examples/sum.bench.py
Running 1 benchmark
-------------------
>>> A simple sum benchmark (x1000)
Sum using sum()............................ ✔ 0.00142s / 0.00000s
Sum using +=............................... ✔ 0.00245s / 0.00000s
✔ Done
Hooks¶
The Benchmark
provide some hooks as methods:
before_class()
: executed once before each classbefore()
: executed once before each methodbefore_each()
: executed before each method callafter_class()
: executed once after each classafter()
: executed once after each methodafter_each()
: executed after each method call
from minibench import Benchmark
class MyBench(Benchmark):
def before_class(self):
# Will be executed once before all class methods
pass
def before(self):
# Will be executed once before each method
pass
def before_each(self):
# Will be executed before each method call
pass
def after_class(self):
# Will be executed once after all class methods
pass
def after(self):
# Will be executed once after each method
pass
def after_each(self):
# Will be executed aftereach method call
pass