Files
toropovv-pp_122-mko-lab2/lec1/vasilijt-lec1/bench.py
T

14 lines
556 B
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import statistics
import time
def bench(func, *args, repeats=5, warmup=1):
"""Возвращает медиану, минимум и максимум времени func(*args), с."""
for _ in range(warmup): # прогрев: кэши, JIT
func(*args)
times = []
for _ in range(repeats):
start = time.perf_counter() # монотонный таймер высокого разрешения
func(*args)
times.append(time.perf_counter() - start)
return statistics.median(times), min(times), max(times)