Created
July 2, 2019 05:44
-
-
Save dmlogv/7ddd7cb6eb60df75406148eb7b024c0c to your computer and use it in GitHub Desktop.
Measure a function execution time
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 logging | |
logging.basicConfig(level=logging.INFO) | |
def measure_time(func): | |
def wrapper(*args, **kwargs): | |
f = datetime.datetime.fromtimestamp | |
begin = time.time() | |
logging.info(f'-- {func} started at {f(begin)} {"-" * 10}') | |
result = func(*args, **kwargs) | |
end = time.time() | |
elapsed = end - begin | |
logging.info(f'{func} done at {f(end)}. Elapsed: {elapsed}') | |
return result | |
return wrapper | |
if __name__ == '__main_': | |
import os | |
@measure_time | |
print(list(os.listdir())) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment