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.)

Data for this lab

Your program reads its data from standard input. Download the file, then give it to your program as shown below. Start with data set 1: its expected output is given.

os1-4-data-1.txt

L1_cache 1 ns
RAM_access 100 ns
SSD_read 100 us
edge_round_trip 2 ms
cloud_round_trip 100 ms
What your program must print for os1-4-data-1.txt
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

Run your program first, then open this and compare line by line: your output must contain these lines exactly.

How to give the file to my program

Terminal (macOS, Linux, or Windows Command Prompt), after compiling:

java Main < os1-4-data-1.txt

Windows PowerShell does not understand <:

Get-Content os1-4-data-1.txt | java Main

IntelliJ IDEA: Run → Edit Configurations → Redirect input from. Or simply paste the content of the file into the console when the program waits for input.

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.