diff --git a/tools/stopwatch.py b/tools/stopwatch.py index ce2fcea..eb059bc 100644 --- a/tools/stopwatch.py +++ b/tools/stopwatch.py @@ -2,16 +2,23 @@ from time import perf_counter_ns from .types import IntOrNone -def ns_to_string(ns: int) -> str: +def get_time_string_from_ns(ns: int) -> str: + # mis, ns = ns // 1000, ns % 1000 + # ms, mis = mis // 1000, mis % 1000 + # s, ms = ms // 1000, ms % 1000 + # m, s = s // 60, s % 60 + # h, m = m // 60, m % 60 + # d, h = h // 24, h % 24 + units = ['ns', 'µs', 'ms', 's'] unit = 0 while ns > 1_000: - ns /= 1_000 - unit += 1 - if unit == len(units) - 1: + if unit > 3: break + ns /= 1000 + unit += 1 - return f"{ns:1.2f}{units[unit]}" + return "%1.2f%s" % (ns, units[unit]) class StopWatch: @@ -26,26 +33,26 @@ class StopWatch: self.started = perf_counter_ns() self.stopped = None - def stop(self) -> int: + def stop(self) -> float: self.stopped = perf_counter_ns() return self.elapsed() reset = start - def elapsed(self) -> int: + def elapsed(self) -> float: if self.stopped is None: return perf_counter_ns() - self.started else: return self.stopped - self.started - def avg_elapsed(self, divider: int) -> int: - return self.elapsed() // divider # in ns precision loosing some rounding error probably will not hurt + def elapsed_string(self) -> str: + return get_time_string_from_ns(self.elapsed()) - def elapsed_string(self): - return ns_to_string(self.elapsed()) + def avg_elapsed(self, divider: int) -> float: + return self.elapsed() / divider def avg_string(self, divider: int) -> str: - return ns_to_string(self.avg_elapsed(divider)) + return get_time_string_from_ns(int(self.avg_elapsed(divider))) def __str__(self): return self.avg_string(1)