Skip to content

Instantly share code, notes, and snippets.

Last active Aug 29, 2015
What would you like to do?
Python class for elapsed time in sec or msec
class Timer(object):
def __init__(self, verbose=False):
self.verbose = verbose
def __enter__(self):
self.start = time.time()
return self
def __exit__(self, *args):
self.end = time.time()
self.secs = self.end - self.start
self.msecs = self.secs * 1000
if self.verbose:
print 'elapsed time: %f ms' % self.msecs
# example of usage
if __name__ == '__main__':
with Timer() as t:
# do something long or not
print t.secs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment