A loop reuses the same few variables thousands of times. Which kind of locality is that?
Temporal locality: reusing the same data soon after.
- Temporal locality ✔
- Spatial locality
- Sequential locality
Why: Temporal locality means using the same data again soon, so it is still in cache. Spatial locality means using data stored next to what you just used, like walking through an array. Code with good locality gets far more cache hits.
Huh, didn't know that: The instructions in a loop body show temporal locality too, because the same instructions run every pass.
Why does temporal locality love reruns? It always wants to see the same thing again.
References
Answer: Temporal locality. Temporal locality means using the same data again soon, so it is still in cache. Spatial locality means using data stored next to what you just used, like walking through an array. Code with good locality gets far more cache hits.
- OpenStax Introduction to Computer Science 5.5: Memory Hierarchy openstax.org/books/introduction-computer-science/pages/5-5-memory-hierarchy
if we reuse the data as much as possible, we get better performance because the data is available in the cache
Checked 2026-10-10.
Think this answer is wrong? Tap "Challenge this answer" on the card in the game and tell us why.
More stuff everyone should know
That's stuff everyone should know.