Algorithms and Data Structures · Senior · Question 51 of 75

What is cache-friendly about iterating a 2D array row by row in C-like layouts?

Explanation

With row-major layout a[i][j] and a[i][j+1] are neighbors. Iterating by columns jumps a whole row each step and misses the cache, which can be several times slower for the same O(n²) work.

Further reading

Problem with a question

Algorithms and Data Structures · Senior · question 51

Test yourself

Take the Algorithms and Data Structures · Senior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz