What does "cache-friendly code" mean and how does it affect C++ performance?
Hi! I'm learning C++ and came across the term "cache-friendly". I often see in performance discussions that code should be cache-friendly, but I don't fully understand what this means in practice.
I have a simple example. I'm working with 1000×1000 matrices (int). Here are two code variants for summing them:
Variant 1: by rows
```cpp
int sum = 0;
for (int i = 0; i < 1000; ++i)
for (int j = 0; j < 1000; ++j)
sum += matrix[i][j];
```
Variant 2: by columns
```cpp
int sum = 0;
for (int j = 0; j < 1000; ++j)
for (int i = 0; i < 1000; ++i)
sum += matrix[i][j];
```
The first variant runs 5–10 times faster on my computer! I've read that this is related to caching, but why such a huge difference? How do you even determine whether code is cache-friendly?
No answers yet.
Log in to leave an answer