简体   繁体   English

给定代码段中所有出现的参考时间和空间局部性

[英]All occurrences of temporal and spatial locality of reference in a given code snippet

I have read about spatial and temporal localities. 我读过关于空间时间 地方。

In a couple of words. 用几句话。

Temporal locality : Programs often access the same memory locations repeatedly. 时间局部性 :程序经常重复访问相同的存储器位置。

Spatial locality : Programs also often access adjacent memory locations repeatedly. 空间局部性 :程序还经常重复访问相邻的内存位置。

Now I'm tying to analyse the following code to find all the occurrences of temporal and spatial locality of reference. 现在,我要分析以下代码,以查找参考的时空局部性的所有情况。

for (int i = 0, j = 10; i < 100; i++)
    a[i] = j++;

I figured out only the followings. 我只发现以下内容。

Spatial 空间空间

  • a[i] = j++; After referencing a[i] we are about to reference a[i+1] . 引用a[i]我们将引用a[i+1]
  • The instructions to do all this are stored in next to each other in memory . 执行所有这些操作的指令彼此相邻地存储在内存中

Temporal

  • i compared against 100. i比较了100。
  • i incremented by the one: i++ . i增加了一个: i++
  • The assignment a[i] = j++ uses i as array index. 赋值a[i] = j++使用i作为数组索引。
  • Array base a used for indexing each a[i] . 数组基a用于索引每个a[i]
  • j incremented by the one in assignment a[i] = j++ . j在赋值a[i] = j++递增1。

So am I correct in all of these and did I miss something else? 那么我在所有这些方面都正确吗,我还错过了其他事情吗?

Definitions are the other way around. 定义则相反。

Spatial locality - locality in space, accessing nearby memory locations. 空间局部性-空间局部性,访问附近的内存位置。

Temporal locality - locality in time, accessing the same location several times. 时间位置-时间上的位置,多次访问同一位置。

The use of spatial locality in designing caches is such that, when your program needs a[i] , cache just doesn't fetch a[i] from memory. 在设计缓存时使用空间局部性是这样的,当程序需要a[i] ,缓存就不会从内存中获取a[i] It fetches few entries a[i], a[i+1], a[i+2] ... as much as the size of a cacheline. 它获取的条目a[i], a[i+1], a[i+2] ...数量与缓存行的大小一样多。 By doing so, cache expect you are likely to access a[i+1] soon. 这样,缓存期望您很快就会访问a[i+1] And when you do so, cache does not need to fetch it from memory, its already there thanks to the use of spatial locality. 而且,这样做时,缓存不需要从内存中获取,这要归功于使用空间局部性。

Temporal locality comes when you access i and j . 当您访问ij时会出现时间局部性。 Every time you need i and j , no need to fetch it from memory, as it is cached. 每次您需要ij ,都不需要从内存中获取它,因为它已被缓存。 By caching, i and j , cache thought you are gonna access it again, so saves time to fetch it from memory. 通过缓存ij ,缓存以为您将再次访问它,因此节省了从内存中获取它的时间。

And to answer the exact point, yes you have identified places for spatial and temporal locality. 为了回答确切的问题,是的,您已经确定了空间和时间局部性的位置。 Only thing that is not correct is, your statement, " The instructions to do all this are stored in next to each other in memory ". 唯一不正确的是,您的语句“ 将所有操作的指令并排存储在内存中 ”。 There wont be separate instructions to access a[i] , a[i+1] . 将没有单独的指令来访问a[i]a[i+1] It will be a single instruction used inside a loop, using memory address calculated each time. 这将是循环内使用的一条指令,使用每次计算的内存地址。 As I explained above, the advantage is that the cache does not need to fetch the data for the next iteration as it is likely to fetched as part of an early iteration in the loop. 正如我在上面解释的那样,优点是高速缓存不需要为下一次迭代获取数据,因为它很可能作为循环中早期迭代的一部分而获取。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM