Last active
January 13, 2020 17:27
-
-
Save thetonus/e86fdc13c7dc6536ada7063369321978 to your computer and use it in GitHub Desktop.
Timing Decorator
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
""" Simple timing decorator """ | |
from functools import wraps | |
from time import time | |
__all__ = ("timer") | |
def timer(f): | |
@wraps(f) | |
def wrap(*args, **kwargs): | |
start = time() | |
result = f(*args, **kwargs) | |
total = time() - start | |
print(f'func: {f.__name__} took: {total:2.4f} sec') | |
return result | |
return wrap | |
if __name__ == "__main__": | |
""" Test timer function """ | |
@timer | |
def test(): | |
import time | |
time.sleep(2.5) | |
test() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment