Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@wolph
Created October 11, 2014 10:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wolph/3deeec98aed8c7e56172 to your computer and use it in GitHub Desktop.
Save wolph/3deeec98aed8c7e56172 to your computer and use it in GitHub Desktop.
Python timer generator for simple duration measurement
import datetime
def timer(print_=True):
last = datetime.datetime.now()
while True:
now = datetime.datetime.now()
delta = now - last
if print_:
print 'Duration: %s' % delta
yield delta
last = now
if __name__ == '__main__':
import time
t = timer()
print 'Roughly zero'
t.next()
time.sleep(1)
print 'Roughly one'
t.next()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment