Measure and compare function execution times
pip install funcmeasure
# or
pip3 install funcmeasure
from funcmeasure import measure, FunctionStats, TableFormat
def f1():
5**2
def f2():
5**2**10
def f3():
5**2**2**2
# stats = measure([f1, (f2, 'second'), f3], times=1000)
# or
stats = measure(
{
f1: None,
f2: 'second',
f3: None
},
times=1000
)
# prints
#
# Ran 3 functions. 1000 times each.
#
# ╒════╤════════╤════════════╤══════════════╤═════════════╤══════════════╤═════════════╕
# │ │ Name │ Avg (ms) │ Total (ms) │ Best (ms) │ Worst (ms) │ Benchmark │
# ╞════╪════════╪════════════╪══════════════╪═════════════╪══════════════╪═════════════╡
# │ 0 │ f3 │ 0.002123 │ 2.122589 │ 0.001892 │ 0.007872 │ │
# ├────┼────────┼────────────┼──────────────┼─────────────┼──────────────┼─────────────┤
# │ 1 │ f1 │ 0.002282 │ 2.281747 │ 0.001952 │ 0.062483 │ ~1.07x │
# ├────┼────────┼────────────┼──────────────┼─────────────┼──────────────┼─────────────┤
# │ 2 │ second │ 0.004946 │ 4.946447 │ 0.004395 │ 0.053278 │ ~2.33x │
# ╘════╧════════╧════════════╧══════════════╧═════════════╧══════════════╧═════════════╛