Skip to content

Instantly share code, notes, and snippets.

@cypreess
Last active November 2, 2018 08:31
Show Gist options
  • Save cypreess/1bd7f5a2328c755c31a33974e4491e92 to your computer and use it in GitHub Desktop.
Save cypreess/1bd7f5a2328c755c31a33974e4491e92 to your computer and use it in GitHub Desktop.
import time
import datetime
class MeasureDuration:
def __init__(self, title: str = "Total time taken %s"):
self.start = None
self.end = None
self.title = title
def __enter__(self):
self.start = time.time()
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self.end = time.time()
print(self.title % self.duration())
def duration(self):
return str(datetime.timedelta(seconds=(self.end - self.start)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment