Created
June 23, 2014 06:58
-
-
Save shimizukawa/59c5e38314320d563d91 to your computer and use it in GitHub Desktop.
small implementation for time scaling context : 時間計測するcontext
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 | |
import contextlib | |
class TimeIt(object): | |
def __init__(self): | |
self._start = 0 | |
self._end = 0 | |
def begin(self): | |
self._start = time.time() | |
return self._start | |
def finish(self): | |
self._end = time.time() | |
return self._end | |
@property | |
def time(self): | |
if self._end: | |
end = self._end | |
else: | |
end = time.time() | |
return end - self._start | |
def __enter__(self): | |
self.begin() | |
return self | |
def __exit__(self, *tb): | |
self.finish() | |
def __str__(self): | |
return '{:.2f} secs'.format(self.time) | |
if __name__ == '__main__': | |
with TimeIt() as ti: | |
time.sleep(1) | |
print(ti.time) | |
time.sleep(2) | |
print(ti.time) | |
time.sleep(3) | |
print(ti.time) | |
print(ti) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment