Analyse code for spatial and temporal locality

一曲冷凌霜 提交于 2019-12-18 13:26:03

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/4092019/analyse-code-for-spatial-and-temporal-locality

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!