Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>In addition to @Marc Claesen's answer, I think that an instructive classic example of cache-unfriendly code is code that scans a C bidimensional array (e.g. a bitmap image) column-wise instead of row-wise.</p> <p>Elements that are adjacent in a row are also adjacent in memory, thus accessing them in sequence means accessing them in ascending memory order; this is cache-friendly, since the cache tends to prefetch contiguous blocks of memory.</p> <p>Instead, accessing such elements column-wise is cache-unfriendly, since elements on the same column are distant in memory from each other (in particular, their distance is equal to the size of the row), so when you use this access pattern you are jumping around in memory, potentially wasting the effort of the cache of retrieving the elements nearby in memory.</p> <p>And all that it takes to ruin the performance is to go from</p> <pre><code>// Cache-friendly version - processes pixels which are adjacent in memory for(unsigned int y=0; y&lt;height; ++y) { for(unsigned int x=0; x&lt;width; ++x) { ... image[y][x] ... } } </code></pre> <p>to</p> <pre><code>// Cache-unfriendly version - jumps around in memory for no good reason for(unsigned int x=0; x&lt;width; ++x) { for(unsigned int y=0; y&lt;height; ++y) { ... image[y][x] ... } } </code></pre> <p>This effect can be quite dramatic (several order of magnitudes in speed) in systems with small caches and/or working with big arrays (e.g. 10+ megapixels 24 bpp images on current machines); for this reason, if you have to do many vertical scans, often it's better to rotate the image of 90 degrees first and perform the various analysis later, limiting the cache-unfriendly code just to the rotation.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload