分析代码的空间和时间局部性
您好,有一些关于空间和时间局部性的问题。我在课程理论中读过,
空间局部性
如果引用一个项目,则附近的其他地址很快就会被引用的可能性
时间局部性
在某个时间点被引用的一项往往很快就会再次被引用。
好的,但是我如何在代码中看到它呢?我想我理解了时间局部性的概念,但我还不理解空间局部性。例如,在这个循环中,
for(i = 0; i < 20; i++)
for(j = 0; j < 10; j++)
a[i] = a[i]*j;
当访问 a[i] 十次时,内部循环将调用相同的内存地址,所以我猜这是时间局部性的一个例子。但上述循环中是否也存在空间局部性?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当然。例如,在引用 a[5] 后,您将引用 a[6]。
Of course. For instance, after referencing a[5] you are about to reference a[6].