from time import perf_counter class SequentialTimer: def __init__(self, make_print=False): self.timings = [] self.make_print = make_print def time(self, message: str): if self.make_print: print(message) self.timings.append((perf_counter(), message)) def to_str(self) -> str: s = "" if len(self.timings) <= 1: s = "No timings" return s t0 = self.timings[0][0] for ((t1, m1), (t2, _)) in zip(self.timings, self.timings[1:]): s += f"TIME: step: {t2 - t1:06.3f} | cum {t2 - t0:06.3f} - {m1}\n" s += f"ALL TIME: {self.timings[-1][0] - self.timings[0][0]:07.3f}\n" return s def printall(self): print(self.to_str())