1
0
mirror of https://github.com/vladmandic/sdnext.git synced 2026-01-27 15:02:48 +03:00
Files
sdnext/modules/timer.py
Vladimir Mandic 4cef19892e segment prototype
2024-01-13 21:20:33 -05:00

36 lines
876 B
Python

import time
class Timer:
def __init__(self):
self.start = time.time()
self.records = {}
self.total = 0
def elapsed(self):
end = time.time()
res = end - self.start
self.start = end
return res
def record(self, category, extra_time=0):
e = self.elapsed()
if category not in self.records:
self.records[category] = 0
self.records[category] += e + extra_time
self.total += e + extra_time
def summary(self, min_time=0.05):
res = f"{self.total:.2f} "
additions = [x for x in self.records.items() if x[1] >= min_time]
if not additions:
return res
res += " ".join([f"{category}={time_taken:.2f}" for category, time_taken in additions])
return res
def reset(self):
self.__init__()
startup = Timer()