Last active
November 19, 2020 02:59
-
-
Save rcgale/219491786f8686a52e6afa6b93c38108 to your computer and use it in GitHub Desktop.
Timer decorator (`@timed()`)
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
| def timed(long_running_seconds=0.0, file=None): | |
| import sys | |
| import functools | |
| import time | |
| def decorator_timed(func): | |
| @functools.wraps(func) | |
| def wrapper_timed(*args, **kwargs): | |
| start = time.process_time() | |
| result = func(*args, **kwargs) | |
| end = time.process_time() | |
| duration = end - start | |
| if duration >= long_running_seconds: | |
| print(f'Function {func.__name__} took {duration:.03f}', file=file or sys.stderr) | |
| return result | |
| return wrapper_timed | |
| return decorator_timed |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment