THINK FIRST·CODE LATER

← All labs

Latency numbers on a human scale

Problem

Engineers remember approximate latencies ("an L1 cache hit is about a nanosecond, a round trip to a distant cloud about 100 ms"). They are easier to feel on a human scale: pretend 1 nanosecond lasts 1 second.

Input: lines operation latency unit until the end of input, where unit is ns, us (microseconds), ms or s. Operation names have no spaces.

For each line print operation: X ns -> human scale: Y, where X is the latency in nanoseconds (as a whole number) and Y is X seconds expressed in the largest fitting unit among years (365 days), days, hours, minutes, seconds, with one decimal and the unit name: e.g. 3.2 years, 4.6 days, 2.8 hours, 1.7 minutes, 100.0 seconds. Use years if X ≥ 31,536,000; days if X ≥ 86,400; hours if X ≥ 3,600; minutes if X ≥ 60; otherwise seconds.

Finally print Slowest/fastest ratio: R — the ratio between the largest and smallest latency, rounded to the nearest whole number.

Input:

L1_cache 1 ns
RAM_access 100 ns
SSD_read 100 us
edge_round_trip 2 ms
cloud_round_trip 100 ms

Output:

L1_cache: 1 ns -> human scale: 1.0 seconds
RAM_access: 100 ns -> human scale: 1.7 minutes
SSD_read: 100000 ns -> human scale: 1.2 days
edge_round_trip: 2000000 ns -> human scale: 23.1 days
cloud_round_trip: 100000000 ns -> human scale: 3.2 years
Slowest/fastest ratio: 100000000

(The latencies are rough, typical orders of magnitude, not exact measurements.)

Write it here or in your IDE, then paste it. Compile and test it yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.