Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Aptitude
  • Engineering Mathematics
  • Discrete Mathematics
  • Operating System
  • DBMS
  • Computer Networks
  • Digital Logic and Design
  • C Programming
  • Data Structures
  • Algorithms
  • Theory of Computation
  • Compiler Design
  • Computer Org and Architecture
Open In App
Next Article:
Difference Between Spatial Locality and Temporal Locality
Next article icon

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 LocalityTemporal 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
Difference Between Spatial Locality and Temporal Locality

V

vasu_gupta
Improve
Article Tags :
  • Computer Organization & Architecture
  • Operating Systems
  • Difference Between
  • GATE CS

Similar Reads

    Difference between Spatial and Temporal Data Mining
    Data Mining is an information discovery process that integrates the organization of large databases to discover implicit patterns that have significant value. Spatial data mining and temporal data mining are two important sub-disciplines in data mining as both of them involve data having either spat
    5 min read
    Difference Between SSH Local and Remote Port Forwarding
    SSH stands for "Secure Shell" or "Secure Socket Shell". It is a cryptographic network protocol that allows two computers to communicate and share the data over on insecure network such as the Internet. SSH protocols protect the network from various attacks. Local Port ForwardingLocal Port Forwarding
    6 min read
    Difference between Memory and Storage
    Memory and storage are fundamental components of a computer system, each serving distinct purposes. Understanding the difference between them helps in optimizing system performance and making informed decisions about hardware.Memory (RAM) is responsible for temporarily holding active data that our c
    4 min read
    Difference Between Instant and Instance
    The terms "instant" and "instance" may appear similar but have distinct meanings and usage. "Instant" is an adjective that refers to a precise moment in time, while "instance" refers to a particular occurrence or example of something.Definition of "Instant""Instant" is an adjective that refers to a
    3 min read
    Difference between Round trip time (RTT) and Time to live (TTL)
    1. Round Trip Time (RTT) :The length of time taken by a data packet to be sent to a destination including the time it takes for an acknowledgement of that packet to be received back at the origin place. image showing RTT 2. Time to live (TTL) :The lifespan or life time of data that is being sent. On
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences