Hi have some question regarding spatial and temporal locality. I have read in the course theory that
If one item is referenced, the likelihood of other address close by will be referenced soon
One item that is referenced at one point in time it tend to be referenced soon again.
Ok, but how do I see that in the code? I think I understood the concept for temporal locality but I don't understand spatial locality yet. For instance in this loop
for(i = 0; i < 20; i++)
for(j = 0; j < 10; j++)
a[i] = a[i]*j;
The inner loop will call same memory address when accessing a[i] ten times so that's an example for temporal locality I guess. But is there spatial locality also in the above loop?
There are two basic types of reference locality – temporal and spatial locality. Temporal locality refers to the reuse of specific data and/or resources within a relatively small time duration. Spatial locality (also termed data locality) refers to the use of data elements within relatively close storage locations.
The outer loop is an example of spacial locality. It sequentially increments the address the inner for-loop calls. The inside loop demonstrates temporal locality. The exact same memory address is accessed ten times in a row, and multiplied by j each time.
Temporal locality is exploited by having a cache between the proces- sor and the memory that is big enough so that a word gets accessed multiple times before getting evicted. Spatial lo- cality is exploited by using blocks spanning multiple words to move data between the different layers of the hierarchy.
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. 2. It refers to the tendency of execution which involve a number of memory locations .
Of course. For instance, after referencing a[5] you are about to reference a[6].
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With