Created
October 11, 2014 10:47
-
-
Save wolph/3deeec98aed8c7e56172 to your computer and use it in GitHub Desktop.
Python timer generator for simple duration measurement
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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