Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@dmoisset
Created March 10, 2014 22:11
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmoisset/9475548 to your computer and use it in GitHub Desktop.
Save dmoisset/9475548 to your computer and use it in GitHub Desktop.
A small timer useful for testing
import contextlib
from datetime import datetime
class Timer(object):
def start(self):
self.start = datetime.now()
def stop(self):
self.stop = datetime.now()
def elapsed(self):
return self.stop - self.start
def assertHasDate(self, date):
assert self.start <= date <= self.stop, \
"%s was not during the timer" % date
def assertNotHasDate(self, date):
assert not (self.start <= date <= self.stop), \
"%s was during the timer" % date
@contextlib.contextmanager
def timekeeper():
t = Timer()
t.start()
try:
yield t
finally:
t.stop()
if __name__ == "__main__":
t1 = Timer()
t1.start()
for i in range(100000):
pass
t1.stop()
print "elapsed", t1.elapsed()
with timekeeper() as t2:
d = datetime.now()
t2.assertHasDate(d)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment