Skip to content

Instantly share code, notes, and snippets.

@pengmeng
Last active January 7, 2016 06:10
Show Gist options
  • Select an option

  • Save pengmeng/78a25663c20ab8890b81 to your computer and use it in GitHub Desktop.

Select an option

Save pengmeng/78a25663c20ab8890b81 to your computer and use it in GitHub Desktop.
A timer context manager
__author__ = 'mengpeng'
import time
class timeme(object):
__unitfactor = {'s': 1,
'ms': 1000,
'us': 1000000}
def __init__(self, unit='s', precision=4):
self.start = None
self.end = None
self.total = 0
self.unit = unit
self.precision = precision
def __enter__(self):
if self.unit not in timeme.__unitfactor:
raise KeyError('Unsupported time unit.')
self.start = time.time()
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self.end = time.time()
self.total = (self.end - self.start) * timeme.__unitfactor[self.unit]
self.total = round(self.total, self.precision)
def __str__(self):
return 'Running time is {0}{1}'.format(self.total, self.unit)
@minghu6
Copy link
Copy Markdown

minghu6 commented Jan 7, 2016

通过ssh https clone都被拒绝

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment