Open In App

Difference Between Spatial Locality and Temporal Locality

Last Updated : 12 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In computer systems, the concept of locality of reference is used to increase the efficiency of the cache memory by trying to predict the further usage of the data. The locality of reference is divided into two types: the two categories of locality; spatial locality and temporal locality. The distinction between these two representations of locality is quite critical in enhancing cache efficiency and the overall procedure of the system.

Spatial locality is the characteristic of a program whereby it accesses memory locations that are adjacent to each other in the physical profile within a given time frame. Temporal locality means the probability of the program having access to the same memory address at some period.

Both concepts are used by compilers, processors, and cache memory systems to increase cache Hit Rate with the help of pre-fetching data into the cache memory so the access time is reduced and the execution time is faster.

Spatial Locality

Spatial Locality means that all those instructions that are stored nearby to the recently executed instruction have a high chance of execution. It refers to the use of data elements(instructions) that are relatively close in storage locations.

Advantages of Spatial Locality

  • Improved Cache Utilization: There is better cache predictability in spatial locality because data blocks are preloaded into the cache and hence more accesses to memory are not required.
  • Optimized Data Prefetching: When spatial locality is present, systems can prefetch data into the cache in advance that in the subsequent cache memory addresses near to the current memory address will be required. This minimizes the number of cycles attributable to memory access times.
  • Efficient for Sequential Access Patterns: Sequential data access patterns in loops that access an array, for instance, reap a lot of benefits from a pro-active spatial locality, where data in the cache is precooked for future access by loading it with nearby data.

Disadvantages of Spatial Locality

  • Limited Benefit for Non-Sequential Access: Nonsequential access is another issue that may not improve with enhanced spatial locality because preloading adjacent data may prove costly if callers never use them.
  • Increased Cache Misses in Complex Access Patterns: If the program makes random or unpredictable accesses to the program, then spatial locality may become detrimental because it may bring in cache misses, in data that is not useful anymore.

Temporal Locality

Temporal Locality means that an instruction that is recently executed has a high chance of execution again. So the instruction is kept in cache memory such that it can be fetched easily and takes no time to search for the same instruction.

Advantages of Temporal Locality

  • Ideal for Repetitive Operations: Generally, loop types show good temporal locality as the same data and instruction are used repeatedly by the application during its’ execution.
  • Enhanced Performance for Frequently Used Data: Temporal locality frequently accessed data is kept within the cache and system performance is enhanced and delays normally attribute to data retrieved from main memory are reduced.
  • Reduced Cache Misses: The first property called temporal locality, ensures a very limited number of cache misses since any data that was recently used, is likely to be used again, and therefore is in the cache, and does not need to be fetched again from memory.

Disadvantages of Temporal Locality

  • Limited to Frequent Reuse: While temporal locality only favors programs which have repetitive usage of the same data or instruction. Nevertheless, temporal locality is sometimes ineffective in contexts where data is invoked sparingly or in annunciation since the required data is not necessarily next to each other in memory.
  • Cache Eviction Issues: If the cache is filled with more information by presumptions of temporal locality, it would lead to the premature removal of important data from the cache leading to cache misses.

Difference Between Spatial Locality and Temporal Locality

Spatial Locality Temporal Locality
In Spatial Locality, nearby instructions to recently executed instruction are likely to be executed soon. In Temporal Locality, a recently executed instruction is likely to be executed again very soon.
It refers to the tendency of execution which involve a number of memory locations . It refers to the tendency of execution where memory location that have been used recently have a access.
It is also known as locality in space. It is also known as locality in time.
It only refers to data item which are closed together in memory. It repeatedly refers to same data in short time span.
Each time new data comes into execution. Each time same useful data comes into execution.
Example: Data elements accessed in array (where each time different (or just next)
element is being accessing ).
Example: Data elements accessed in loops (where same data elements are accessed multiple times).

Conclusion

Hence spatial local as well as temporal local are two important factors that determine the efficiency of the cache memory in computer systems. Spatial locality is more on caching block which has been anticipated that it will be requested in the near future; it is therefore ideal for sequential access. Meanwhile, temporal locality focuses on the cache storing the data which are most used frequently; this is advantageous to programs that frequently reference the same memory addresses.



Next Article

Similar Reads