Skip to content

Instantly share code, notes, and snippets.

@messiest
Last active December 5, 2019 20:22
Show Gist options
  • Save messiest/c6c86d60dd5544783d5eb2be448cfb8a to your computer and use it in GitHub Desktop.
Save messiest/c6c86d60dd5544783d5eb2be448cfb8a to your computer and use it in GitHub Desktop.
import time
from contextlib import ContextDecorator
class Timer(ContextDecorator):
def __enter__(self):
self.start = time.time()
return self
def __exit__(self, *exc):
print("RUNTIME: ", self.format_runtime(time.time() - self.start))
return False
def format_runtime(self, runtime):
if runtime > 60:
mins = runtime // 60
secs = runtime % 60
return f"{mins} minutes, {secs} seconds"
else:
return f"{runtime:.1f} seconds"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment