1total = 02for n in nums:3 total += n5–10 ms4–16 TB~50 µs~1 TB50–100 nsgigabytes10–20 nstens of MB3–10 ns~1 MB~1 ns~64 KB<1 ns~1 KBCORE✓ hit✗ miss✓ hit✗ miss✓ hit✗ miss✓ hit✗ missprice?pricenums[0-7]
Each step of the pyramid is one level of memory: its time to answer and its size are on its frontWhite token: a request; khaki token: the value (or a whole 64-byte cache line) coming backKhaki boxes on the terraces: copies kept by the cachesHit: the level has the data · Miss: ask the next level downSeven levels of memory, fastest and smallest on top: registers, three SRAM caches, DRAM main memory, an SSD and a hard disk (typical numbers; not to scale). A request falls down the levels until one has the data; on the way back each cache keeps a copy. Because programs reuse data (temporal locality) and use nearby data (spatial locality), most requests hit the fast levels. SRAM keeps each bit in a loop of about six transistors and needs no refresh; DRAM keeps each bit as charge on one capacitor behind one transistor, so it is denser and cheaper but slower (Patterson and Hennessy, Computer Organization and Design, sections 5.1-5.2).

The levels

  1. A computer keeps data in several kinds of memory, stacked here like a pyramid. The top levels are tiny but very fast. Each level down holds much more, costs less per byte, and takes much longer to answer.

A request falls down

Why caches work

Numbers and code

;; step 1/11 · pyramid
;; about: Registers, L1 cache, L2 cache, L3 cache, RAM (DRAM), SSD (flash), Hard disk
level        time        size
registers    <1 ns       ~1 KB
L1 cache     ~1 ns       ~64 KB
L2 cache     3–10 ns     ~1 MB
L3 cache     10–20 ns    tens of MB
RAM (DRAM)   50–100 ns   gigabytes
SSD (flash)  ~50 µs      ~1 TB
hard disk    5–10 ms     4–16 TB

1 ns = a billionth of a second
1 µs = 1,000 ns · 1 ms = 1,000,000 ns
(typical numbers; the drawing is not to scale)

Typical sizes and access times after Hennessy and Patterson, Computer Architecture: A Quantitative Approach (6th ed., figure 2.1): registers about 1000 bytes and 300 ps, L1 64 KB at about 1 ns, L2 3-10 ns, L3 10-20 ns, main memory 50-100 ns, flash 25-50 microseconds, disk 5-10 ms. Real chips vary; AMD's Ryzen 9 7950X has 1 MB of L2 per core and 64 MB of L3. Average memory access time = hit time + miss rate × miss penalty (Hennessy and Patterson, appendix B). With a 1 ns hit, a 5% miss rate and a 100 ns penalty: 1 + 0.05 × 100 = 6 ns.