Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How does this change lead to different performance?

I’ve just stumbled upon this from 2015 Fall: 15-213 Introduction to Computer Systems lecture by Carnegie Mellon University.

Memory System Performance Example

Is this incorrect? Because it’s exactly the same code with swapped int variables. Please help. I appreciate any explanation here.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Lets take a small and simple "2D" array:

int a[2][2];

In memory it’s laid out like this:

+---------+---------+---------+---------+
| a[0][0] | a[0][1] | a[1][0] | a[1][1] |
+---------+---------+---------+---------+

In the first variant of the loops, you iterate in the order a[0][0], a[0][1], a[1][0], and a[1][1]. You iterate over the elements that are close together, so they are in the cache.

In the second variant you iterate in the order a[0][0], a[1][0], a[0][1], and a[1][1].

See how you jump back and forth in the second variant? When the arrays are larger, this will make the CPU need to fetch data from memory more often, since it’s no longer possible to fit it all into the cache. More memory access leads to lower efficiency and longer execution times.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading