空间与时间局部性 [英] Spatial vs. Temporal locality

查看:625
本文介绍了空间与时间局部性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我理解的术语的定义,但我有自己的应用概念,code麻烦。对于一个练习中,我们被要求描述如果下面code是空间和时间:

I understand the definitions of the terms, but I am having trouble applying their concepts to code. For an exercise, we are asked to describe if the following code is spatial or temporal:

for (int i=0; i<10; i++) {
    printf(some_array[i]);
}

我喜欢这种感觉是空间局部性,因为当数组的一个索引被访问时,下一个索引存储位置将尽快访问的循环迭代。这是看它的正确方法?是什么决定,如果code是时间与空间?更多的例子将是巨大的。

I feel like this is spatial locality because when one index of the array is accessed, the next index memory location will be accessed as soon as the loop iterates. Is this the correct way to look at it? What determines if code is temporal versus spatial? More examples would be great.

推荐答案

这是一个有点傻的锻炼,真的。 code不是时间或空间。

It's a bit of a silly exercise, really. Code is not temporal or spatial.

但时间局部性意味着你要多次访问同一地址,相对紧密的时间。你不这样做,在这里(除非你访问 I ,我猜),因此通过排除法,可以断定这一定是空间位置。

But temporal locality implies that you're going to access the same address multiple times, relatively closely in time. You're not doing that here (unless you count accessing i, I guess), so by a process of elimination, you could conclude that this must be spatial locality.

更多precisely,你所访问 some_array [0] ,那么 some_array [1] ,上等等,等等,这些都是接近在一起的地址空间,所以是的,这可能是靠天吃饭的空间的地方。

More precisely, you're accessing some_array[0], then some_array[1], etc. etc. These are close together in address space, so yes, this may be "relying" on spatial locality.

这篇关于空间与时间局部性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆