Created
December 12, 2018 16:42
-
-
Save cgrinaldi/9e0ae901a9c9d4e42b8f8fba7df0d4cf to your computer and use it in GitHub Desktop.
Timer decorator for function timing
This file contains 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 functools | |
import time | |
def timer(func): | |
"""Print the runtime of a decorated function.""" | |
@functools.wraps(func) | |
def wrapper_timer(*args, **kwargs): | |
start_time = time.perf_counter() | |
value = func(*args, **kwargs) | |
end_time = time.perf_counter() | |
run_time = end_time - start_time | |
print(f'Finished {func.__name__!r} in {run_time:.4f} secs') | |
return value | |
return wrapper_timer | |
def print_time(name, begin_time, end_time): | |
run_time = end_time - begin_time | |
print(f'Finished {name} {run_time:.2f} seconds') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment