Skip to content

Instantly share code, notes, and snippets.

@dmitryhd
Last active July 7, 2016 08:34
Show Gist options
  • Select an option

  • Save dmitryhd/b5eb8ee50f3bc27b9f952d827d7e2e09 to your computer and use it in GitHub Desktop.

Select an option

Save dmitryhd/b5eb8ee50f3bc27b9f952d827d7e2e09 to your computer and use it in GitHub Desktop.
class Timer:
def __init__(self, precision=1):
self.precision = precision
self.creation_time = time.time()
self.start()
def start(self):
self.btime = time.time()
@property
def time(self) -> float:
return round(time.time() - self.btime, self.precision)
@property
def time_total(self) -> float:
return round(time.time() - self.creation_time, self.precision)
def round(self) -> float:
t = self.time
self.start()
return t
# usage
import time
timer = Timer(precision=3)
print(timer.time)
time.sleep(0.1)
print(timer.round())
time.sleep(0.1)
print(timer.round())
print(timer.time_total)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment