Source code for rics.performance._format_perf_counter

from time import perf_counter


[docs]def format_perf_counter(start: float, end: float = None) -> str: """Format performance counter output. This function formats performance counter output based on the time elapsed. For ``t < 120 sec``, accuracy is increased whereas for durations above one minute a more user-friendly formatting is used. Args: start: Start time. end: End time. Set to now if not given. Returns: A formatted performance counter time. Examples: >>> format_perf_counter(0, 309613.49) '3d 14h 0m 13s' >>> format_perf_counter(0, 0.154) '154ms' >>> format_perf_counter(0, 31.39) '31.4s' See Also: :py:func:`time.perf_counter` """ t = (end or perf_counter()) - start return format_seconds(t)
[docs]def format_seconds(t: float, allow_negative: bool = False, brief: bool = True) -> str: """Format performance counter output. This function formats performance counter output based on the time elapsed. For ``t < 120 sec``, accuracy is increased whereas for durations above one minute a more user-friendly formatting is used. Args: t: Time in seconds. allow_negative: If ``True``, format negative `t` with a leading minus sign. brief: If ``True``, Returns: A formatted performance counter time. Raises: ValueError: If ``t < 0`` and ``allow_negative=False`` (the default). """ if t < 0: if not allow_negative: allow_negative = True raise ValueError(f"Refuse to format {t=} < 0; to allow, set {allow_negative=}") return f"-{format_seconds(abs(t))}" # days, hours, minutes, seconds = round(days), round(hours), round(minutes), round(seconds) if t > 60.0: days, seconds = divmod(round(t), 86400) hours, seconds = divmod(seconds, 3600) minutes, seconds = divmod(seconds, 60) parts = (days, hours, minutes, seconds) nonzero = tuple(p > 0 for p in parts) start, stop = nonzero.index(True), len(nonzero) - nonzero[::-1].index(True) return " ".join(f"{parts[i]}{'dhms'[i]}" for i in range(start, stop)) if t >= 1.0: return f"{t:.1f}s" if t > 0.5: return f"{t:.2f}s" if t > 10**-3: return f"{t * 10 ** 3:.0f}ms" if t > 10**-6: # 1 μs return f"{t * 10 ** 6:.0f}μs" if t > 10**-9: return f"{t * 10 ** 9:.0f}ns" return f"{t:.3g}s"