36

I am a little confused on the meanings of spatial and temporal locality. I'm hoping by looking at it with an array example it will help me understand it better.

In an example like this: A[0][1], A[0][2], A[0][3].... etc

Does this demonstrate temporal locality? I see the same row is accessed many times but at different offsets... does this mean a different address is accessed?

Also, am I correct in saying that an example like this: A[1], A[2], A[3]... etc

Demonstrates spatial locality?

Hopefully some clarification on how temporal and spatial locality work in real code will help me better understand them.

6 Answers 6

85

Spatial and temporal locality describe two different characteristics of how programs access data (or instructions). Wikipedia has a good article on locality of reference.

A sequence of references is said to have spatial locality if things that are referenced close in time are also close in space (nearby memory addresses, nearby sectors on a disk, etc.). A sequence is said to have temporal locality if accesses to the same thing are clustered in time.

If a program accesses every element in a large array and reads it once and then moves on to the next element and does not repeat an access to any given location until it has touched every other location then it is a clear case of spatial locality but not temporal locality. On the other hand, if a program spends time repeatedly accessing a random subset of the locations on the array before moving on to another random subset it is said to have temporal locality but not spatial locality. A well written program will have data structures that group together things that are accessed together, thus ensuring spatial locality. If you program is likely to access B soon after it accesses A then both A and B should be allocated near each other.

Your first example

A[0][1], A[0][2], A[0][3]

shows spatial locality, things that are accessed close in time are close in space. It does not show temporal locality because you have not accessed the same thing more than once.

Your second example

A[1], A[2], A[3]

also shows spatial locality, but not temporal locality.

Here's an example that shows temporal locality

A[1], A[2000], A[1], A[1], A[2000], A[30], A[30], A[2000], A[30], A[2000], A[30], A[4], A[4]
0
19

In simple words,

Temporal locality: The concept that a resource that is referenced at one point in time will be referenced again sometime in the near future.

Spatial locality: The concept that likelihood of referencing a resource is higher if a resource near it was just referenced.

Source(s): Wikipedia

8

Here is an example of code with locality:

var sum = 0;
for (i = 0; i < n; i++){
  for(j=0; j < m ; j++){
    sum += a[i][j];
    }
}
return sum;
  • There exists temporal locality because sum is accessed frequently in the loop. Temporal locality is exploited by keeping recently used instruction and data values in cache memory and by exploiting a cache hierarchy. Or even in a register, not in memory at all.

  • There exists spatial locality because we have an array 'a' and we access each element of the array in order. Spatial locality is generally exploited by using larger cache blocks and by incorporating prefetching mechanisms (fetching items of anticipated use) into the cache control logic.

3
  • 1
    I think you reversed the 2nd sentence of each bullet point. And BTW, compilers will keep sum in a register, not memory at all, to get the maximum benefit of repeated access. Jul 24, 2018 at 21:13
  • yeah your right @peterCordes , I will make that adjustment
    – superrcoop
    Jul 25, 2018 at 22:04
  • For future readers, spacial locality occurs here only under the assumption that a is stored in row-major order: a[0][0], a[0][1],...,a[0][m],a[1][0], a[1][1],... Some languages don't specify that their two-dimensional arrays are stored in this way. Fortran, for example, doesn't. In that case to get space locality one needs to exchange the loops. Oct 12, 2019 at 15:12
0

I off and on have difficulty in remembering the difference between them though I remember both types of locality.

Spatical Locality to remember that keep "sequentially" adverb in mind.

Temporal Locality to remember that at the beginning times of learning sorting algos, you see "a temp variable" to swap. e.g. bubble sort. It has two loops and to swap there is like int temp = ......

You can recognize which definition belongs to what by means of the way.

1
  • The terms are based on the standard English meaning of the words. Spatial = in space / position. Temporal = relation in time. Locality = nearness. (Links are to google dictionary). "Temporary" has the same root word as "temporal", Latin "tempus" = time. But other than that, temporary and temporal are different concepts. If that connection / mnemonic trick helps you remember what "temporal" means, then great. Jul 13, 2019 at 16:08
0

Temporal locality: Temporal locality is based on repeatedly referenced resources.

Spatial locality: Spatial locality states that the data adjacent to recently referenced data will be requested in the near future.

-1

Temporal locality is the special case of spatial locality.

3
  • That's sort of true, and an interesting observation, for a cache based on lines / blocks of data, like a CPU cache. (Rather than a list of recent search queries or something, where spatial locality isn't even well defined. But this is a computer-architecture question where caches are pretty much always of some kind of address space, whether it's a TLB, decoded-uop cache, or a data cache). But often spatial locality means the nearby accesses are very nearly simultaneous, and basically part of one larger access. Temporal typically means soon but not necessarily right away. Apr 19, 2018 at 8:03
  • 1
    i.e. you need both spatial and temporal locality to get cache hits, otherwise the line containing the item will have been evicted. But yeah, the case of accessing the same item again is kind of a special case of spatial locality. I'd like to upvote this answer if it said that, please consider an edit :) Apr 19, 2018 at 8:06
  • Why the downvotes? It's correct and also given on en.wikipedia.org/wiki/….
    – Sagar Shah
    Sep 16, 2020 at 5:50

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.