Created
January 21, 2016 16:25
-
-
Save krabello/6338ac491b879e7364d5 to your computer and use it in GitHub Desktop.
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 time | |
class StopWatch: | |
def __init__(self, func=time.perf_counter): | |
self.elapsed = 0.0 | |
self._start = None | |
self._func = func | |
self.maxTime = 10000000 | |
def start(self): | |
if self._start is not None: | |
raise RuntimeError('Already started') | |
self._start = self._func() | |
def stop(self): | |
if self._start is None: | |
raise RuntimeError('Not started') | |
end = self._func() | |
self.elapsed += end - self._start | |
self._start = None | |
def counter(self): | |
while self.maxTime > 0: | |
self.maxTime -= 1 | |
def __exit__(self, *args): | |
self.stop() | |
@property | |
def elapsedTime(self): | |
return self.elapsed | |
sw = StopWatch() | |
sw.start() | |
time.sleep(4) | |
sw.stop() | |
print(sw.elapsedTime) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment