AlgoViz

Estimates in perspective

easy 24 min

The reference numbers behind every estimate: how long memory, disks and networks take, how big common data is, how to turn days into seconds, and the powers of two.

Video lesson · Chapter 1 of 6

Stretch time to see its scale

0:00 / 4:52

If a cache hit took one second

1:15Reel coming soon

Theory

Read or listen first. About 2 min.

What it is

Estimation needs a few reference numbers: how long a step takes, how big a thing is, how many seconds a period has, how many values fit in some bits. With those, most sums are one or two multiplications.

The useful part is the ratio. Memory is about a thousand times quicker than a random read from a solid-state disk, and a trip to another continent is about a thousand times slower than that. Ratios stay useful long after the exact values have changed.

Why it exists

A design choice is often a choice between two places to get something: memory or disk, this machine or another, this region or a far one. If you know the sizes of the gaps, you see which part of a request is slow before you measure anything.

The numbers also catch mistakes. A page that makes forty calls to another region cannot answer in 100 milliseconds, whatever the code looks like.

A short history

  1. 1998

    The International Electrotechnical Commission approves the names kibi, mebi and gibi for powers of 1,024, so that kilo, mega and giga can keep meaning powers of 1,000.

  2. 2001

    Peter Norvig publishes the essay Teach Yourself Programming in Ten Years. Its current version lists approximate times for operations on a typical PC.

  3. 2009

    Jeff Dean's keynote at the LADIS workshop shows "numbers everyone should know", from a cache reference to a packet crossing the Atlantic and back. The list spreads widely under the name latency numbers every programmer should know.

Words to know

Latency
How long one operation takes.
ns, µs, ms
Nano-, micro- and milliseconds: a billionth, a millionth and a thousandth of a second.
L1, L2 cache
Small, very fast memory inside the processor.
Round trip
There and back over a network.
Seek
Moving a hard disk's head to the right place.
GB and GiB
10⁹ bytes and 2³⁰ bytes (1,073,741,824). They differ by 7%.
Order of magnitude
A power of ten.

How it works

Step through the idea, or press Play.

Step 1 of 3

Place it. Cache, memory, disk, next machine or next continent?

Understand it

Start here

Where are the shears?

A gardener needs the shears. In the hand they take a second. Further away they take longer, and the steps are not equal: each place is far worse than the one before. The story below stretches a computer's time, so that the fastest memory takes exactly one second.

One second in the hand
1 secondin the hand

Same request, five places. The distance decides far more than the speed of the gardener.

Origin

Where the numbers come from

The best known list is Jeff Dean's table from talks at Google around 2009. Very similar figures stand in an essay by Peter Norvig. The SSD rows were added by others later. They describe machines of their time, so use them as rules of thumb.

What they are good for

  • Design choices

    Cache or database, sync or async, local or remote.

  • Quick estimates

    Can this design answer in the time we have?

  • Interviews

    You are expected to know orders of magnitude, not datasheets.

  • Debugging

    Compare what you measure with what it should cost.

At a glance

OperationOrder of magnitudeTimes an L1 hit
L1 cache0.5 ns1
L2 cache7 ns14
Main memory (RAM)100 ns200
SSD random read100 µs200,000
Round trip in one data centre0.5 ms1,000,000
Hard disk seek10 ms20,000,000
Round trip across continents150 ms300,000,000
The SSD row is rounded to a power of ten; the longer table below uses 150 µs. Both are the same order of magnitude.

The same numbers on a system

One machine holds a CPU with caches of nanoseconds, memory at 100 nanoseconds and an SSD at 100 microseconds. It calls a Redis cache in the same data centre with a round trip of half a millisecond, and a database in another region with a round trip of 69 milliseconds.
Crossing a box costs more than anything that happens inside it.

Latency

The classic table

OperationClassic valueToday, roughly
L1 cache reference0.5 nsabout 1 ns
Branch mispredict5 nsa few ns
L2 cache reference7 ns3 to 7 ns
Mutex lock and unlock25 ns15 to 25 ns without contention
Main memory reference100 nsabout 100 ns
Compress 1 KB (Zippy, now Snappy)3 µsa few µs or less
Send 1 KB over 1 Gb/s10 µs8 µs by arithmetic; under 1 µs at 10 Gb/s
Read 4 KB at random from SSD150 µsabout 100 µs local, single-digit ms on a cloud disk
Read 1 MB in order from memory250 µs100 µs at 10 GB/s
Round trip in one data centre500 µsunder 1 ms
Read 1 MB in order from SSD1 ms0.3 ms at 3 GB/s
Hard disk seek10 ms5 to 10 ms: physics hardly moved
Read 1 MB in order from hard disk20 ms5 ms at 200 MB/s
Packet California, Netherlands, California150 msabout 150 ms: the speed of light did not change
The right-hand column is our rule of thumb, with the assumed speed where one is used. Measure your own.

Which of them improve?

So a design that avoids distance stays good for years.

Feel it

If an L1 hit took one second

The stretch

stretched seconds=time in ns0.5,1000.5=200 s,150,0000.5=300,000 s=3.5 days\text{stretched seconds} = \frac{\text{time in ns}}{0.5}, \qquad \frac{100}{0.5} = 200\ \text{s}, \quad \frac{150{,}000}{0.5} = 300{,}000\ \text{s} = 3.5\ \text{days}
Every time is multiplied by two billion.
OperationRealStretched
L1 cache0.5 ns1 second
L2 cache7 ns14 seconds
Memory100 ns3 minutes 20 seconds
SSD random read150 µs3.5 days
Round trip in the data centre0.5 ms11.6 days
Hard disk seek10 ms7.6 months
Round trip across continents150 ms9.5 years

Use it

Three sums from one table

Calls one after another

40×0.5 ms=20 msbut40×69 ms=2.8 s40 \times 0.5\,\text{ms} = 20\,\text{ms} \qquad\text{but}\qquad 40 \times 69\,\text{ms} = 2.8\,\text{s}
Forty small calls are harmless inside a data centre and fatal across a continent (69 ms is Microsoft's published median between East US and West US). Batch them, or run them at the same time: 69 ms.

Reading 1 GB in order

1,000×0.25 ms=0.25 s1,000×1 ms=1 s1,000×20 ms=20 s1{,}000 \times 0.25\,\text{ms} = 0.25\,\text{s} \qquad 1{,}000 \times 1\,\text{ms} = 1\,\text{s} \qquad 1{,}000 \times 20\,\text{ms} = 20\,\text{s}
Memory, SSD and hard disk with the classic values.

One request, and where the time goes

Making the cache twice as quick saves 0.25 ms. Moving the data closer saves 68.

Sizes

How big things are

Typical sizes (assumptions to start with)

KindItemSize
TextShort post, 280 characters280 bytes in plain English, up to about 4 times that in other scripts
TextEmail without attachments10 KB
TextJSON response1 to 100 KB
TextE-book, text only1 MB
ImageThumbnail10 KB
ImageCompressed photo100 to 500 KB
ImageHigh-resolution photo2 to 5 MB
ImageRAW photo25 to 50 MB
Audio3-minute song3 to 5 MB (about 128 to 220 kb/s)
Video1 minute at 720p10 to 20 MB (1.3 to 2.7 Mb/s)
Video1 hour HD2 to 4 GB (4 to 9 Mb/s)
Video1 hour 4K10 to 20 GB (22 to 44 Mb/s)
RecordUser profile1 KB
RecordProduct2 to 5 KB
RecordLog line100 bytes to 1 KB
RecordShort link500 bytes

The ladder

UnitBytesAbout
1 byte1one English letter
1 KB1,000a short note
1 MB1,000,000a photo, or a book without pictures
1 GB10⁹1,000 such photos, or half an hour of HD video
1 TB10¹²a million photos, or a small company's database
1 PB10¹⁵a thousand such databases
Each step is 1,000 times the last. KB × thousand is MB, × million is GB, × billion is TB.

Time

Days into seconds

PeriodSecondsIn your head
1 minute6060
1 hour3,6004,000
1 day86,400100,000
1 week604,800600,000
30 days2,592,0002.5 million
1 year31,536,00030 million
5 years157,680,000150 million
10 years315,360,000300 million

Per day to per second

A dayExactDividing by 100,000
1 million12/s10/s
10 million116/s100/s
100 million1,157/s1,000/s
1 billion11,574/s10,000/s
Short horizons (a day) plan capacity, middle ones (a month) plan storage growth, long ones (five years) plan retention.

Bits

Powers of two

Start with 1 and double it ten times: 2, 4, 8, 16, 32, 64, 128, 256, 512, 1,024. That is close to a thousand, and the trick repeats every ten doublings.

PowerValueRemember it as
272^{7}128plain ASCII characters
282^{8}256values of one byte, 0 to 255
2102^{10}1,024a thousand, kibi (Ki)
2162^{16}65,536port numbers
2202^{20}1,048,576a million, mebi (Mi)
2302^{30}1,073,741,824a billion, gibi (Gi)
2322^{32}4,294,967,29632-bit ids: about 4 billion
2402^{40}1.0995 × 10¹²a trillion, tebi (Ti)
2502^{50}1.1259 × 10¹⁵a quadrillion, pebi (Pi)

How far kilo and kibi drift apart

210103=1.024220106=1.049230109=1.0742401012=1.1002501015=1.126\frac{2^{10}}{10^{3}} = 1.024 \quad \frac{2^{20}}{10^{6}} = 1.049 \quad \frac{2^{30}}{10^{9}} = 1.074 \quad \frac{2^{40}}{10^{12}} = 1.100 \quad \frac{2^{50}}{10^{15}} = 1.126
For estimates it does not matter. For a bill or a memory limit it does.

Why powers of two keep turning up

WhereWhyExample
Hash tablesThe remainder becomes a cheap bit operation1,024, 2,048 buckets
Cache linesData is laid out in 64-byte lines on common processorspadding of structs and arrays
Memory allocatorsBlocks of a few fixed sizes are quick to reuse16, 32, 64, 128 bytes
ShardsDoubling splits each shard in two and moves half of its rows4, 8, 16, 32 shards
Doubling is one way to grow shards; moving fewer rows is what consistent hashing is for.

Choose

From a number to a choice

Where should this data live?

Is the estimate off by a unit?

Use the numbers like this

  • "Memory is roughly a thousand times quicker than an SSD."
  • "Forty calls across regions is seconds, so one batch."
  • "About 12 a second per million a day."
  • "Rule of thumb; we will measure it."

Not like this

  • Reciting the table
  • "0.5 ns, so it is fast."
  • Adding three decimals to a guess
  • Quoting a 2009 hard disk for a 2026 SSD
Python
def stretched(ns):    s = ns / 0.5    for name, size in (("years", 31_536_000), ("days", 86_400), ("minutes", 60)):        if s >= size:            return f"{s / size:.1f} {name}"    return f"{s:.0f} seconds"for name, ns in [("L2", 7), ("memory", 100), ("SSD", 150_000),                 ("data centre", 500_000), ("ocean", 150_000_000)]:    print(f"{name:12} {stretched(ns)}")
Stretch any latency. What is 1 ms?
Edit and run it in your browser

Summary

The whole topic in one minute.

Memory is about 100 ns, a random SSD read about 100 µs, a round trip inside a data centre about 0.5 ms, a hard disk seek about 10 ms, and crossing an ocean and back about 150 ms. Remember the steps of a thousand, and treat every digit as a rule of thumb.

Sizes go up by a thousand from KB to PB. A day is 86,400 seconds, so a million a day is about 12 a second. Two to the tenth is about a thousand, and 1 GiB is 7% more than 1 GB.

  • 100 ns memory, 100 µs SSD, 0.5 ms data centre, 10 ms disk seek, 150 ms ocean.
  • Distance stays; devices improve.
  • Count round trips before anything else.
  • 1 million a day is about 12 a second.
  • 2¹⁰ is about a thousand; GiB is 7% more than GB.

Sources