Skip to content

Instantly share code, notes, and snippets.

@cgrinaldi
Created December 12, 2018 16:42
Show Gist options
  • Save cgrinaldi/9e0ae901a9c9d4e42b8f8fba7df0d4cf to your computer and use it in GitHub Desktop.
Save cgrinaldi/9e0ae901a9c9d4e42b8f8fba7df0d4cf to your computer and use it in GitHub Desktop.
Timer decorator for function timing
import functools
import time
def timer(func):
"""Print the runtime of a decorated function."""
@functools.wraps(func)
def wrapper_timer(*args, **kwargs):
start_time = time.perf_counter()
value = func(*args, **kwargs)
end_time = time.perf_counter()
run_time = end_time - start_time
print(f'Finished {func.__name__!r} in {run_time:.4f} secs')
return value
return wrapper_timer
def print_time(name, begin_time, end_time):
run_time = end_time - begin_time
print(f'Finished {name} {run_time:.2f} seconds')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment