Skip to content

Instantly share code, notes, and snippets.

@Michaelliv
Last active September 15, 2020 11:40
Show Gist options
  • Select an option

  • Save Michaelliv/1ebf5ab1394252a3c3672a3eb8d93630 to your computer and use it in GitHub Desktop.

Select an option

Save Michaelliv/1ebf5ab1394252a3c3672a3eb8d93630 to your computer and use it in GitHub Desktop.
Python run time benchmarking utility
import time
from typing import Callable, Set, Iterator, List
def bench(fn: Callable, warm_up_iterations: int = 10, test_iterations: int = 100, verbose: bool = False):
for i in range(warm_up_iterations):
start_time = time.time()
fn()
end_time = time.time() - start_time
if verbose:
print(f"warm up #{i} [{end_time}sec]")
times: List[float] = []
for i in range(test_iterations):
start_time = time.time()
fn()
end_time = time.time() - start_time
times.append(end_time)
if verbose:
print(f"test #{i} [{end_time}sec]")
print(float(sum(times)) / len(times))
return times
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment