Last active
July 21, 2017 14:38
-
-
Save mdellavo/505e20c33c332188a4abefb632bc59e7 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 time | |
from contextlib import contextmanager | |
class TimedResult(object): | |
def __init__(self): | |
self.started = None | |
self.stopped = None | |
def start(self): | |
self.started = time.time() | |
def stop(self): | |
self.stopped = time.time() | |
@property | |
def elapsed(self): | |
return self.stopped - self.started | |
@property | |
def elapsed_millis(self): | |
return self.elapsed * 1000 | |
@contextmanager | |
def timed(): | |
result = TimedResult() | |
result.start() | |
try: | |
yield result | |
finally: | |
result.stop() | |
with timed() as result: | |
time.sleep(1) | |
print "took", result.elapsed_millis |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment