format_time#

scio.utils.misc.format_time(duration, num_digits=4)[source]#

Format a short float duration into a readable str.

Parameters:
  • duration (float) – Expressed in seconds, duration to format. Must satisfy 0 <= duration < 10 ** (n + 1) - 0.5.

  • num_digits (int) – Number of significant digits to display. Larger durations can have one more and shorter durations less (see examples). Must be at least 3. Defaults to 4.

Returns:

out (str) – Formated duration – e.g. "567.9 ms".

Raises:

AssertionError – If either num_digits < 3 or not 0 <= duration < 10 ** (n + 1) - 0.5.

Examples

With num_digits=4:

╭───────────────┬────────────────┬───────────────────────────────────────╮
│   Duration    │     Result     │                  Comment              │
├───────────────┼────────────────┼───────────────────────────────────────┤
│      1.5      │    1.500 s     │ Significant 0's added                 │
│      0.56789  │    567.9 ms    │ Last digit is rounded...              │
│      0.99995  │    1.000 s     │ ...which can lead to precision loss   │
│      0.12345  │    123.4 ms    │ Rounds half to even (python built-in) │
│   1234        │    1234. s     │ Point is added for constant witdh     │
│  12345        │    12345 s     │ One more digit for longer durations   │
│ 123456        │ AssertionError │ Exceeded max duration                 │
│     -1        │ AssertionError │ Negative duration                     │
│      0        │    0.000 as    │ Smallest unit for shorter durations   │
│      5.67e-20 │    0.057 as    │ Precision is worse near 0.            │
╰───────────────┴────────────────┴───────────────────────────────────────╯

Notes

Implementation heavily relies on following facts:

  • Consecutive units have constant ratio of 10**3,

  • Highest unit is the unit of duration’s encoding.