+ 2

What is difference between temporal and spatial locality

Can anyone explain these two topic with example

6th Sep 2019, 4:13 PM
Preity
Preity - avatar
2 Respostas
+ 4
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. 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. 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]
6th Sep 2019, 4:48 PM
sarah
sarah - avatar
+ 3
Temporal means related to time. Spatial means related to space, location etc. Locality means a nearby area.
6th Sep 2019, 11:39 PM
Sonic
Sonic - avatar