What is Temporal locality?
The tendency to access the same data or instructions again soon after a first access, as with variables reused inside a loop. One of the reasons caches work.
Field: Memory Hierarchy
Read the beginner explainer: How Does CPU Cache Work? Hit Rate, Lines and Locality
Japanese name: 時間的局所性
What temporal locality means
A program shows temporal locality when an item it uses is likely to be used again soon. Loop counters, accumulators, frequently called functions and the instructions of a loop body are classic examples: once loaded, they are touched again and again. Caches exploit this by keeping recently used data close to the processor, and replacement policies such as LRU (least recently used) evict the item untouched for longest on the bet that recent use predicts future use. Registers exploit it at the finest scale when the compiler keeps a hot variable out of memory entirely.
Temporal vs. spatial locality
Spatial locality is the tendency to use addresses near one just used, as when an array is read from start to end. Caches exploit it by fetching a whole line, typically 64 bytes, on each miss. Temporal locality is about reuse of the same item over time and is exploited by cache capacity and replacement policy. A single sweep through a huge array has good spatial but no temporal locality; repeatedly updating one sum variable has strong temporal locality; reading random addresses once each has neither.
Reuse distance and why it matters
A useful way to quantify temporal locality is reuse distance: the number of distinct cache lines accessed between two uses of the same line. In a fully associative LRU cache, an access hits if its reuse distance is smaller than the number of lines the cache holds. With L1 reachable in a few cycles, L3 in tens and DRAM taking tens to around 100 ns, hundreds of cycles, keeping reuse distances short often matters more for performance than reducing the raw operation count.
Improving it in code
Programmers and compilers shorten reuse distance by reordering work. Loop blocking (tiling) finishes all work on a cache-sized chunk before moving on, and loop fusion merges two loops that touch the same array so data is reused while still cached. Keeping hot values in local variables lets the compiler hold them in registers. Conversely, data that is written once and not reused soon may be written with non-temporal stores that bypass the cache to avoid evicting useful lines.
Try it: questions about Temporal locality
- AReading each element of an array once, from start to end
- BUpdating the same sum variable many times inside a loop
- CReading data at a random address only once each time
Show answer and explanation
Answer: B. Updating the same sum variable many times inside a loop
Temporal locality is the tendency to reuse data soon after using it, and a variable read and written repeatedly inside a loop is the classic example. Such data is reused while still in the cache or registers, so accesses are fast. Reading an array from start to end is an example of spatial locality, using nearby addresses in succession, which is a different property. Reading random addresses once each has almost no locality of either kind.
FAQ
What is an example of temporal locality?
Updating the same sum variable in every iteration of a loop, or executing the same loop instructions repeatedly; the same item is reused soon after its last use.
What is the difference between temporal and spatial locality?
Temporal locality is reusing the same data again soon; spatial locality is using data at nearby addresses next, such as walking through an array.
How do caches exploit temporal locality?
They keep recently used data in fast memory and evict the least recently used items first, so data that is reused soon is likely still present.
Books on Memory Hierarchy Ad
As an Amazon Associate I earn from qualifying purchases.
Comparisons that include this term
This page was drafted with AI and fact-checked by a separate AI review before publication. Our Editorial policy explains how we write and check content. If you spot an error, please tell us on our Contact page.
Back to the glossary