Boss lesson · Unit 02
Arrays versus linked lists, for real
Why the cache decides which O(n) is fast
What you practise
- Cache lines, hits and misses, spatial and temporal locality, why row-major order beats column-major and a contiguous array beats pointer chasing despite the same Big-O
Try a question from this lesson
Pick one
Summing 10 million numbers is O(n) whether they sit in an array or in a linked list. On a real machine, which is faster?
Correct answer
The array, often several times faster
Why
Big-O counts steps, not what each step costs. Array elements sit side by side; list nodes are scattered over the heap. This lesson shows why that changes the price of every step.
The ideas
Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.
Concept
The cache
RAM is slow next to the CPU, so the CPU keeps small, fast copies of recently used memory: the caches.
When data is not in the cache (a miss), the CPU fetches it from RAM, and not just one byte: a whole cache line of 64 bytes, neighbours included.
CPU register instant
L1 cache about 1 ns
L2 / L3 cache about 4-40 ns
RAM about 100 nsConcept
Locality
Caches reward two habits.
Spatial locality: after using some data, you use its neighbours. Walking an array in order is the perfect case, and the CPU even fetches ahead when it spots the pattern.
Temporal locality: you reuse the same data again soon, while it is still cached.
10 more exercises in the app
- 3× type the answer
- 3× multiple choice
- 1× match the pairs
- 1× fill the blanks
- 1× order the lines
- 1× true or false
Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.