Skip to content

Instantly share code, notes, and snippets.

@HebeHH
Created August 27, 2020 07:55
Show Gist options
  • Save HebeHH/52c91226bc71c8560069895abf544c48 to your computer and use it in GitHub Desktop.
Save HebeHH/52c91226bc71c8560069895abf544c48 to your computer and use it in GitHub Desktop.
Timer class for profiling
class Timer:
def __init__(self, indent = 30):
self.start = time.time()
self.rj = indent
self.report = 'Start:'.rjust(self.rj) + '0.000000'.rjust(10)
self.current = self.start
def diff(self, msg):
tmp = self.current
self.current = time.time()
return msg.rjust(self.rj) +':'+str(round(self.current - tmp, 6)).rjust(10)
def total(self, msg):
self.current = time.time()
return msg.rjust(self.rj) +':'+str(round(self.current - self.start, 6)).rjust(10)
def pdiff(self, msg):
print(self.diff(msg))
def ptotal(self, msg):
print(self.total(msg))
def build_report(self, msg):
self.report += '\n' + self.diff(msg)
def get_report(self):
return self.report + '\n' + self.total("Total time taken")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment