开发者

Efficent Cache usage in C

I want to write code that best uses the cache memory of my system. For example, I have one large array(2kb size) that is frequently used in operations. For better开发者_如何学编程 execution speed, I want it to be loaded in chache memory so that It takes less time for processor to fetch it. How can I ensure this thing in C language? Any help would be appreciated.


First, ask what processor you are running your code on. Then read its tech specs to find out how big the cache is and how the cache is arranged.

CPU caches are pretty big these days so if your array is only 2kB then it's almost certainly going to be held entirely in cache unless you read megabytes of data between accesses to the array.

In short: don't worry about it. Your array is tiny so it's unlikely you can do much to "optimise" cache usage for it. Instead, look at the algorithm you are using to see if there is a more efficient approach that can be used, and run a profiler on your code to see where the bottlenecks are.


For GCC you could use "-fprefetch-loop-arrays" option. Anyway, if you're using that memory block frequently, it will most probably reside in cache.

If you want to make it faster, read this: http://www.agner.org/optimize/optimizing_cpp.pdf , and try to use it. And don't forget, the best optimizations can be done by changing the algorithm.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜