tags:

views:

86

answers:

2

Hello people!

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.

+6  A: 

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.

Jason Williams
+1  A: 

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.

ruslik