C ++:使用CPU缓存 [英] C++: Working with the CPU cache

查看:137
本文介绍了C ++:使用CPU缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在CPU缓存上阅读维基百科: http://en.wikipedia.org/wiki/CPU_cache# Replacement_Policies

I was reading the wikipedia on the CPU cache here: http://en.wikipedia.org/wiki/CPU_cache#Replacement_Policies


将一些内存区域标记为不可缓存可以提高性能,通过避免缓存很少重新映射的内存区域,访问。这避免了将某些内容加载到缓存中而不重新使用的开销。

Marking some memory ranges as non-cacheable can improve performance, by avoiding caching of memory regions that are rarely re-accessed. This avoids the overhead of loading something into the cache, without having any reuse.

现在,我一直在阅读和学习如何编写程序具有更好的缓存性能(一般考虑,通常不是特定于C ++),但我不知道高级代码可以与CPU缓存行为显式交互。所以我的问题是,有没有办法做我在文章中引用的C ++?

Now, I've been reading and learning about how to write programs with better cache performance (general considerations, usually not specific to C++), but I did not know that high-level code can interact with CPU caching behavior explicitly. So my question, is there a way to do what I quoted from that article, in C++?

此外,我将感谢资源如何提高缓存性能专门在C + ,即使它们不使用任何直接处理CPU高速缓存的功能。例如,我想知道是否使用过多的间接层(例如,指针容器指针的容器)可能会损害缓存性能。

Also, I would appreciate resources on how to improve cache performance specifically in C++, even if they do not use any functions that deal directly with the CPU caches. For example, I'm wondering if using excessive levels of indirection (eg., a container of pointers to containers of pointers) can damage cache performance.

推荐答案

在Windows上,您可以使用 VirtualProtect( ptr,length,PAGE_NOCACHE,& oldFlags) 来设置内存的缓存行为,以避免缓存。

On Windows, you can use VirtualProtect(ptr, length, PAGE_NOCACHE, &oldFlags) to set the caching behavior for memory to avoid caching.

indirections:是的,如果您经常访问不同的内存块(这是通常发生的),它们可能会损坏缓存性能。但请注意,如果您始终取消引用相同的设置, 8块存储器,并且只有第9块不同,则它通常不会产生差别,因为8个块将在第一次存取之后被缓存。

Regarding too many indirections: Yes, they can damage cache performance, if you access different pieces of memory very often (which is what happens usually). It's important to note, though, that if you consistently dereference the same set of e.g. 8 blocks of memory, and only the 9th block differs, then it generally won't make a difference, because the 8 blocks would be cached after the first access.

这篇关于C ++:使用CPU缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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