Skip to content

stop printing "humanized" time units #125

@StefanKarpinski

Description

@StefanKarpinski

On Slack today, some timings were posted:

julia> @btime sum(a) > 0
  26.323 μs (1 allocation: 16 bytes)
false

julia> @btime any(a .> 0)
  116.147 μs (5 allocations: 16.64 KiB)
true

julia> @btime any(!iszero, a)
  178.574 ns (0 allocations: 0 bytes)
true

The entire ensuing conversation was predicated on the fact that any(!iszero, a) was mysteriously slower than both sum(a) > 0 and any(a .> 0). Nobody noticed that the last time measurement is in nanoseconds while the first two are in microseconds. I've seen this happen quite often. It's happened to me. The reason I knew to be alert to this is because I've missed the very subtle difference between "ms", "μs" and "ns" before. Moreover, this doesn't match how Base prints times anymore:

julia> @time sleep(1)
  1.047126 seconds (114.42 k allocations: 6.038 MiB, 0.90% gc time)

julia> @time sleep(0.001)
  0.006039 seconds (82 allocations: 5.375 KiB)

In fact, Base's time printing was changed for the exact same reason! I propose that we choose one of the following ways to print times instead:

  0.000026323 seconds (1 allocation: 16 bytes)
  0.000116147 seconds (5 allocations: 16.64 KiB)
  0.000000179 seconds (0 allocations: 0 bytes)

In other words, with a fixed 9 decimal digits and room for up to three leading digits. That's plenty of digits and it's very clear what scale values are on. If the values must be scaled somehow, then consider using engineering notation instead, so something like this:

  26.323e-6 seconds (1 allocation: 16 bytes)
 116.147e-6 seconds (5 allocations: 16.64 KiB)
 179.000e-9 seconds (0 allocations: 0 bytes)

But note that this is not much shorter and far less clear than just printing 9 digits after the decimal.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions