长数组缓存问题
我最近注意到,用循环扫描长数组会使缓存的性能下降。你们能解释一下为什么会发生这种情况以及解决这个问题的方法吗?我正在使用 C/C++ 在 Linux 平台上工作
I noticed lately that scanning long arrays with a loop makes the performance of caching decrease. Can you guys please explain why this happens and what are some work arounds of this problem. I'm working on a linux platform with C/C++
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个典型的缓存问题:如果循环一个足够大的数组,则每个内存引用都是已刷新的项目,因此需要将该项目从 RAM 提取到缓存。在最简单的场景中,您只需访问比缓存大一个字节的数组即可看到这种情况发生。
即使是复杂的、预测性的获取方案也会受到这个问题的影响。无论缓存方案是什么,总是有可能设计出一种获取模式,该模式将导致每个内存引用上的缓存验证错误。
好消息是,现代缓存系统正在努力减少这种影响。例如,您正在使用的系统可能会在大型数组中进行一些前向提取,因此它不会在每个内存访问上出现错误。最后,花时间了解缓存系统,也许复习一下你在本科时所学的架构课上的笔记:-)将帮助你使用缓存系统。
This is a classic caching issue: if you loop over a large enough array, every memory reference is to an item that has already been flushed, and therefore requires that the item be fetched from RAM to cache. In the simplest scenario, you need only access an array that is one byte larger than your cache to see this happen.
Even sophisticated, predictive fetching schemes are subject to this issue. No matter what the caching scheme, it is always possible to design a fetching pattern that will result in a cache-validation fault on every memory reference.
The good news is that modern caching systems work hard to reduce the impact of this. For example, the system you are using probably does some forward fetching in your large arrays, so it isn't faulting on every memory access. In the end, taking time to understand caching systems, maybe reviewing your notes from the architecture class you slept through in undergrad :-) will help you work with the caching system.