Skip to content

Instantly share code, notes, and snippets.

@logarytm
Created May 27, 2017 18:26
Show Gist options
  • Select an option

  • Save logarytm/5aa63727451ea53d735d81502c0cf137 to your computer and use it in GitHub Desktop.

Select an option

Save logarytm/5aa63727451ea53d735d81502c0cf137 to your computer and use it in GitHub Desktop.
import datetime
import time
class stopwatch(object):
def __init__(self, jobname):
self.jobname = jobname
def __enter__(self):
self.starttime = (datetime.datetime.utcnow() - datetime.datetime(1970, 1, 1)).total_seconds()
def __exit__(self, type, value, traceback):
self.endtime = (datetime.datetime.utcnow() - datetime.datetime(1970, 1, 1)).total_seconds()
print(f'{self.jobname} took {round(self.endtime - self.starttime, 2)}s')
with stopwatch('wow intensive computations'):
time.sleep(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment