Last active
September 15, 2020 11:40
-
-
Save Michaelliv/1ebf5ab1394252a3c3672a3eb8d93630 to your computer and use it in GitHub Desktop.
Python run time benchmarking utility
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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