具有数组的时间与空间位置

时间:2022-08-23 03:50:39

I am a little confused on the meanings of spatial and temporal locality. I'm hoping by looking at it with an array example it will help me understand it better.

我对空间和时间局部的含义有点困惑。我希望通过数组示例查看它,这将有助于我更好地理解它。

In an example like this: A[0][1], A[0][2], A[0][3].... etc

在这样的例子中:A [0] [1],A [0] [2],A [0] [3] ......等

Does this demonstrate temporal locality? I see the same row is accessed many times but at different offsets... does this mean a different address is accessed?

这是否证明了时间局部性?我看到同一行被多次访问但是在不同的偏移处...这是否意味着访问了不同的地址?

Also, am I correct in saying that an example like this: A[1], A[2], A[3]... etc

另外,我说的是这样的例子:A [1],A [2],A [3] ......等等

Demonstrates spatial locality?

展示空间位置?

Hopefully some clarification on how temporal and spatial locality work in real code will help me better understand them.

希望对实时代码中时空局部性如何工作的一些澄清将有助于我更好地理解它们。

3 个解决方案

#1


54  

Spatial and temporal locality describe two different characteristics of how programs access data (or instructions). Wikipedia has a good article on locality of reference.

空间和时间局部性描述了程序如何访问数据(或指令)的两个不同特征。*有一篇关于参考地点的好文章。

A sequence of references is said to have spatial locality if things that are referenced close in time are also close in space (nearby memory addresses, nearby sectors on a disk, etc.). A sequence is said to have temporal locality if accesses to the same thing are clustered in time.

如果在时间上接近参考的事物在空间中也很接近(附近的存储器地址,磁盘上的附近扇区等),则称一系列参考具有空间局部性。如果对同一事物的访问在时间上聚集,则称序列具有时间局部性。

If a program accesses every element in a large array and reads it once and then moves on to the next element and does not repeat an access to any given location until it has touched every other location then it is a clear case of spatial locality but not temporal locality. On the other hand, if a program spends time repeatedly accessing a random subset of the locations on the array before moving on to another random subset it is said to have temporal locality but not spatial locality. A well written program will have data structures that group together things that are accessed together, thus ensuring spatial locality. If you program is likely to access B soon after it accesses A then both A and B should be allocated near each other.

如果一个程序访问一个大型数组中的每个元素并读取它一次然后移动到下一个元素并且不再重复访问任何给定位置,直到它触及其他每个位置,那么它是空间局部性的明显情况但不是时间局部性。另一方面,如果程序花费时间重复访问阵列上的位置的随机子集,然后移动到另一个随机子集,则称其具有时间局部性但不具有空间局部性。编写良好的程序将具有将一起访问的内容组合在一起的数据结构,从而确保空间局部性。如果您的程序很可能在访问A后很快访问B,那么A和B都应该彼此分配。

Your first example

你的第一个例子

A[0][1], A[0][2], A[0][3]

shows spatial locality, things that are accessed close in time are close in space. It does not show temporal locality because you have not accessed the same thing more than once.

显示空间局部性,在时间上接近的事物在空间中很近。它没有显示时间局部性,因为您没有多次访问同一个事物。

Your second example

你的第二个例子

A[1], A[2], A[3]

also shows spatial locality, but not temporal locality.

还显示空间局部性,但不显示时间局部性。

Here's an example that shows temporal locality

这是一个显示时间局部性的例子

A[1], A[2000], A[1], A[1], A[2000], A[30], A[30], A[2000], A[30], A[2000], A[30], A[4], A[4]

#2


10  

In simple words,

简单来说,

Temporal locality: The concept that a resource that is referenced at one point in time will be referenced again sometime in the near future.

时间局部性:在不久的将来某个时间再次引用在某个时间点引用的资源的概念。

Spatial locality: The concept that likelihood of referencing a resource is higher if a resource near it was just referenced.

空间局部性:如果仅引用其附近的资源,则引用资源的可能性更高的概念。

Source(s): Wikipedia

来源:*

#3


0  

Temporal locality is the special case of spatial locality.

时间局部性是空间局部性的特例。

#1


54  

Spatial and temporal locality describe two different characteristics of how programs access data (or instructions). Wikipedia has a good article on locality of reference.

空间和时间局部性描述了程序如何访问数据(或指令)的两个不同特征。*有一篇关于参考地点的好文章。

A sequence of references is said to have spatial locality if things that are referenced close in time are also close in space (nearby memory addresses, nearby sectors on a disk, etc.). A sequence is said to have temporal locality if accesses to the same thing are clustered in time.

如果在时间上接近参考的事物在空间中也很接近(附近的存储器地址,磁盘上的附近扇区等),则称一系列参考具有空间局部性。如果对同一事物的访问在时间上聚集,则称序列具有时间局部性。

If a program accesses every element in a large array and reads it once and then moves on to the next element and does not repeat an access to any given location until it has touched every other location then it is a clear case of spatial locality but not temporal locality. On the other hand, if a program spends time repeatedly accessing a random subset of the locations on the array before moving on to another random subset it is said to have temporal locality but not spatial locality. A well written program will have data structures that group together things that are accessed together, thus ensuring spatial locality. If you program is likely to access B soon after it accesses A then both A and B should be allocated near each other.

如果一个程序访问一个大型数组中的每个元素并读取它一次然后移动到下一个元素并且不再重复访问任何给定位置,直到它触及其他每个位置,那么它是空间局部性的明显情况但不是时间局部性。另一方面,如果程序花费时间重复访问阵列上的位置的随机子集,然后移动到另一个随机子集,则称其具有时间局部性但不具有空间局部性。编写良好的程序将具有将一起访问的内容组合在一起的数据结构,从而确保空间局部性。如果您的程序很可能在访问A后很快访问B,那么A和B都应该彼此分配。

Your first example

你的第一个例子

A[0][1], A[0][2], A[0][3]

shows spatial locality, things that are accessed close in time are close in space. It does not show temporal locality because you have not accessed the same thing more than once.

显示空间局部性,在时间上接近的事物在空间中很近。它没有显示时间局部性,因为您没有多次访问同一个事物。

Your second example

你的第二个例子

A[1], A[2], A[3]

also shows spatial locality, but not temporal locality.

还显示空间局部性,但不显示时间局部性。

Here's an example that shows temporal locality

这是一个显示时间局部性的例子

A[1], A[2000], A[1], A[1], A[2000], A[30], A[30], A[2000], A[30], A[2000], A[30], A[4], A[4]

#2


10  

In simple words,

简单来说,

Temporal locality: The concept that a resource that is referenced at one point in time will be referenced again sometime in the near future.

时间局部性:在不久的将来某个时间再次引用在某个时间点引用的资源的概念。

Spatial locality: The concept that likelihood of referencing a resource is higher if a resource near it was just referenced.

空间局部性:如果仅引用其附近的资源,则引用资源的可能性更高的概念。

Source(s): Wikipedia

来源:*

#3


0  

Temporal locality is the special case of spatial locality.

时间局部性是空间局部性的特例。