Analyse code for spatial and temporal locality
Asked Answered
M

1

7

Hi have some question regarding spatial and temporal locality. I have read in the course theory that

spatial locality

If one item is referenced, the likelihood of other address close by will be referenced soon

temporal locality

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?

Mathewmathews answered 3/11, 2010 at 21:52 Comment(0)
A
6

Of course. For instance, after referencing a[5] you are about to reference a[6].

Andrej answered 3/11, 2010 at 22:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.