问题描述
您对空间和时间局部性有一些疑问.我在课程理论中读到了
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;
当访问a [i]十次时,内部循环将调用相同的内存地址,所以我想这是时间局部性的一个示例.但是上面的循环中还有空间局部性吗?
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?
推荐答案
当然可以.例如,在引用a [5]之后,您将引用a [6].
Of course. For instance, after referencing a[5] you are about to reference a[6].
这篇关于分析代码的空间和时间局部性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!