Quiz Entry - updated: 2026.07.07
What are temporal and spatial locality, and why do caches exploit them?
Temporal locality: recently used data is likely reused soon. Spatial locality: data near a just-used address is likely used next. Caches bet on both.
* Temporal locality reuses the same address soon, while spatial locality gets nearby addresses for free because a whole cache line is loaded at once. *
Examples in code:
// Temporal locality - 'sum' accessed every iteration
for (i = 0; i < N; i++)
sum += a[i]; // 'sum' is reused many times
// Spatial locality - consecutive array elements
for (i = 0; i < N; i++)
// Access a[0], a[1], a[2]...
a[i] = b[i] + c[i];
How caches exploit locality:
- Temporal: Keep recently accessed data in cache (LRU replacement)
- Spatial: Load whole cache lines (64 bytes), not single bytes
Cache line effect:
- You access
a[0](4 bytes) - Cache loads
a[0]througha[15](64 bytes / 4 = 16 ints) - Next 15 accesses are "free" - already in cache!
Why this matters:
- Sequential array access: ~1 cache miss per 16 elements
- Random access: ~1 cache miss per element (16x more misses!)
Tip: "Cache-friendly code" means code that exhibits good locality.
Go deeper:
Locality of reference (Wikipedia) — temporal (reuse soon) vs spatial (nearby addresses) locality and why caches exploit both.
CPU cache (Wikipedia) — cache lines and the memory-hierarchy design that turns locality into speed.