Memory Hierarchy
Which is the best example of 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.
- A. Reading in order from the start is an example of spatial locality.
- B. Correct. Using the same variable many times in a short span is temporal locality.
- C. Reading random addresses once each has almost no locality.