Locality of Reference (original) (raw)

Last Updated : 30 Oct, 2025

CPU performance is limited by slower main memory access. Cache memory improves speed by storing frequently used data and instructions. Its efficiency relies on the Locality of Reference principle.

main_memory

Cache Memory

Formal Definition

Locality of reference is the property of a program that describes how memory references are grouped together in time and space during execution.

Types of Locality of Reference

There are mainly two types of locality of reference:

**Temporal Locality (Locality in Time)

Temporal locality states that if a particular memory location (data or instruction) is accessed at one time, it is likely to be accessed again in the near future.

current_data_stored

Temporal Caching

**Example:

for (int i = 0; i < 100; i++) { sum = sum + a[i]; }

**Cache Operation Based on Temporal Locality:

When a CPU fetches a data item from main memory, it also keeps a copy in the cache, assuming it will be needed soon. If that assumption is correct, subsequent accesses result in a cache hit.

Spatial Locality (Locality in Space)

Spatial locality states that if a particular memory location is accessed, the nearby memory locations are also likely to be accessed soon. Instructions and data are stored in contiguous memory locations.

cpu_access

Spatial Caching

**Example:

for (int i = 0; i < 10; i++) { printf("%d", arr[i]); }

**Cache Operation Based on Spatial Locality:

Cache memory utilizes block fetching. When one word of a block is accessed, the entire block (containing nearby addresses) is loaded into cache, anticipating future accesses.

Mathematical Representation of Average Memory Access Time (AMAT)

The performance of a cache memory system is often evaluated using the **Average Memory Access Time (AMAT). It represents the average time required by the CPU to access data or instructions, considering both cache hits and cache misses.

Let:

Then, the average memory access time can be expressed as:

Average Memory Access Time (AMAT)= h × Tc + (1−h) × Tm

Here,

A higher hit ratio (h) indicates better cache utilization and stronger locality of reference, resulting in lower AMAT and improved overall system performance.