Boss lesson · Unit 03
Where the data sits
Cache-friendly order, and a file larger than memory
What you practise
- Contiguous arrays against scattered objects, why traversal order matters to the CPU cache, processing a file larger than memory
Try a question from this lesson
Pick one
A grid of numbers is stored row after row in one block of memory. You sum it once row by row and once column by column. Which is faster on a real CPU?
Correct answer
Row by row
Why
Row by row reads memory in the order it is laid out, so each trip to RAM also brings in the next several values. Column by column jumps a whole row ahead on every read and keeps missing the CPU cache.
The idea
Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.
Concept
Locality
Reading from RAM is slow next to the CPU itself: on the order of a hundred times slower than reading a value already in the CPU's cache.
So the CPU fetches memory in blocks. Ask for one value and its neighbours arrive with it.
Code that walks memory in order gets most of its reads for free. Code that hops around pays for a fresh block almost every time.
Same number of steps, very different speed.
10 more exercises in the app
- 3× type the answer
- 4× multiple choice
- 1× order the lines
- 1× true or false
- 1× fill the blanks
Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.