Skip to content

Instantly share code, notes, and snippets.

@clayg
Created September 30, 2010 03:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save clayg/603968 to your computer and use it in GitHub Desktop.
Save clayg/603968 to your computer and use it in GitHub Desktop.
from time import time
class StopWatch():
"""
Like using a stopwatch to time things, use start and stop methods,
restart will reset the timer
"""
def __init__(self, start=False):
"""New timers are not started by default
"""
if not start:
now = time()
self._counter = 0.0
self._start = now
self._stop = now
else:
self.restart()
def restart(self):
"""Reset timer to zero and start
"""
self._counter = 0.0
self._start = time()
self._stop = None
def _get_end(self):
return self._stop if self._stop else time()
@property
def time(self):
"""Current elapsed time on the clock in seconds as a float
"""
end = self._get_end()
return end - self._start + self._counter
def start(self):
"""Continue elapsed time
"""
self._counter = self.time
self._start = time()
self._stop = None
def stop(self):
"""Multiple calls to stop won't do anything
"""
self._stop = self._get_end()
def __str__(self):
return str(self.time)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment