Skip to content

Instantly share code, notes, and snippets.

@rcgale
Last active November 19, 2020 02:59
Show Gist options
  • Select an option

  • Save rcgale/219491786f8686a52e6afa6b93c38108 to your computer and use it in GitHub Desktop.

Select an option

Save rcgale/219491786f8686a52e6afa6b93c38108 to your computer and use it in GitHub Desktop.
Timer decorator (`@timed()`)
def timed(long_running_seconds=0.0, file=None):
import sys
import functools
import time
def decorator_timed(func):
@functools.wraps(func)
def wrapper_timed(*args, **kwargs):
start = time.process_time()
result = func(*args, **kwargs)
end = time.process_time()
duration = end - start
if duration >= long_running_seconds:
print(f'Function {func.__name__} took {duration:.03f}', file=file or sys.stderr)
return result
return wrapper_timed
return decorator_timed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment