Created
December 18, 2020 19:59
-
-
Save po5i/f68325e213b040d98b2d74133701c951 to your computer and use it in GitHub Desktop.
Time measure decorator for Python
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
from functools import wraps | |
from time import process_time | |
def measure(func): | |
@wraps(func) | |
def _time_it(*args, **kwargs): | |
start = int(round(process_time() * 1000)) | |
try: | |
return func(*args, **kwargs) | |
finally: | |
end_ = int(round(process_time() * 1000)) - start | |
print( | |
f"Total execution time {func.__name__}: {end_ if end_ > 0 else 0} ms" | |
) | |
return _time_it |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment