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 th开发者_JAVA技巧is 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?
Of course. For instance, after referencing a[5] you are about to reference a[6].
精彩评论