Skip to content
Tactiverge

Computer architecture

Memory hierarchy and caches

Also called: cache · L1 cache · hit rate · locality · cache miss

Definition

Computers use a hierarchy of memories from tiny fast registers and caches to large slow main memory and storage; a cache keeps recently used data close to the processor.

Picture it

Programs tend to reuse the same data (temporal locality) and data stored next to it (spatial locality). A cache bets on that. When the bet wins, it is a hit; when it loses, a miss means a slow trip to main memory. Looping through an array in order is cache-friendly; jumping randomly is not.

The maths

hit rate = hits / accesses
miss rate = 1 − hit rate
SI units used in this entry
QuantityUnitSymbol
percentageper cent%
timeseconds

Worked example

A program makes 2000 memory accesses, of which 1900 hit the cache. What are the hit and miss rates?

  1. Hit rate = 1900 / 2000 = 0.95 = 95 %
  2. Miss rate = 5 %

Answer95 % hits, 5 % misses.

✓ The number in this answer is recomputed from scratch by an automated test.

Common misconceptions

  • Misconception: A bigger cache is not always faster: it can take longer to search.
  • Misconception: Caches are invisible to correctness on a single core but very visible to timing.