Skip to content

Instantly share code, notes, and snippets.

@dmlogv
Created July 2, 2019 05:44
Show Gist options
  • Save dmlogv/7ddd7cb6eb60df75406148eb7b024c0c to your computer and use it in GitHub Desktop.
Save dmlogv/7ddd7cb6eb60df75406148eb7b024c0c to your computer and use it in GitHub Desktop.
Measure a function execution time
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