Computer Science I / Cache and the Memory Hierarchy
Practice question · Multiple choice

Traversing a 2D array row by row can run several times faster than column by column, with identical operation counts. Why?

Hints
  1. Ask what physically sits next to element [i][j] in memory.
  2. A cache line holds 64 bytes. How much of it does a column-wise step use?
Show the answer

B. Because row-major storage makes row-wise access walk contiguous memory

Why

Row-major means [i][j] and [i][j+1] are neighbours, so one cache line serves several iterations; stepping down a column loads a line and uses one element of it. Same instructions, different memory behaviour, which is why loop interchange is a standard optimisation and why the language's storage order is worth knowing.

Read the lesson: Cache and the Memory Hierarchy →

Practise Cache and the Memory Hierarchy

The app has 7 more questions on this lesson, and keeps your place in the course. Computer Science I is free to start.

More questions on Cache and the Memory Hierarchy