缓存是如何工作的? [英] How cache memory works?

查看:111
本文介绍了缓存是如何工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天,当我在电脑组织课上,老师讲到我的一些有趣的事情。当谈到谈论为什么高速缓冲存储器的作品,他说:

Today when I was in computer organization class, teacher talked about something interesting to me. When it comes to talk about Why cache memory works, he said that:

for (i=0; i<M; i++)
   for(j=0; j<N; j++)
      X[i][j] = X[i][j] + K; //X is double(8 bytes)

这是不好的改变与第二的第一行。请问您对这个观点?而且为什么是这样呢?

it is not good to change the first line with the second. What is your opinions on this? And why it is like that?

推荐答案

引用的局部性。由于数据是按行存储,每一行的第j列中相邻的存储器地址。操作系统将典型地从存储器加载整个页到缓存和相邻地址引用将可能指的是相同的页面。如果您在内环行索引增量很可能这些行会在不同的页面(因为它们是用j分隔每个双打)和缓存可能要不断引进和丢弃的内存页,因为它引用数据。这就是所谓的颠簸,是不好的表现。

Locality of reference. Because the data is stored by rows, for each row the j columns are in adjacent memory addresses. The OS will typically load an entire page from memory into the cache and adjacent address references will likely refer to that same page. If you increment by the row index in the inner loop it is possible that these rows will be on different pages (since they are separated by j doubles each) and the cache may have to constantly bring in and throw away pages of memory as it references the data. This is called thrashing and is bad for performance.

在实践和与较大的,现代的高速缓存,该行的尺寸/列将需要相当大的在此之前将开始发挥作用,但它仍然是很好的做法。

In practice and with larger, modern caches, the sizes of the rows/columns would need to be reasonably large before this would come into play, but it's still good practice.

答案以上是针对C,并且允许其他语言的不同。唯一的一个,我知道不同的是FORTRAN。在列优先顺序FORTRAN存储的东西(上面是一排大),这将是正确的改变在FORTRAN的发言顺序。如果你想/需要效率,但重要的是要知道你的语言是如何实现数据存储。

The answer above is specific to C and may differ for other languages. The only one that I know is different is FORTRAN. FORTRAN stores things in column major order (the above is row major) and it would be correct to change the order of the statements in FORTRAN. If you want/need efficiency, it's important to know how your language implements data storage.

这篇关于缓存是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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