Work backwards
Same Big-O, 144 times slower
Summing four million numbers took 144 times longer in one layout than another, though both are O(n).
The result
Volume 1's data-structures lab summed the same 4 million values three ways: from an array, from a linked list whose nodes sit in memory in order, and from the same linked list with its nodes shuffled. Every version does O(n) work. The slowest took 144 times as long as the fastest (the lab).
Big-O counts operations. Something else is being counted here.
Work back from it
First question: which version was fastest, and which slowest? Guess before opening.
The array was fastest and the shuffled list slowest; the in-order list sat between them. If your guess was the other way round, hold on to why: it's the assumption this page takes apart.
Step 1: what does the CPU have to do for each element in each version?
Array: add the next value, whose address is the previous address plus 8. List: load the node, add its value, then load
the next pointer to learn where the following node is. The list's next address isn't known until the current load
finishes.
Step 2: why would "not knowing the next address" matter, if each load is one instruction?
Because a load's cost depends on where the data is. One from the L1 cache takes about 1.6 ns on the lab machine; one that has to go to main memory took 100 to 165 ns (the memory hierarchy, measured). The same instruction varies by about 100 times.
Step 3: so why is the array, which also touches every value, fast?
Memory moves in 64-byte cache lines, so one line brings 8 values, and the hardware prefetcher recognises a sequential walk and fetches lines before they're asked for (cache locality). The core almost never waits.
Step 4: and the shuffled list?
Each next points somewhere unpredictable, so there's no pattern to prefetch, and the next load can't even start until
this one returns its pointer. Nearly every element pays the full trip to main memory, one after another. The in-order
list is slower than the array (one node per line, more traffic) but its addresses still form a pattern the prefetcher can follow.
The root cause
Dependent loads to unpredictable addresses defeat both caching and prefetching, so each step pays main-memory latency. Big-O assumes every memory access costs the same; on real hardware the cost ranges over two orders of magnitude, and the layout of data decides which end you pay.
What follows from it
- "Use a linked list for fast insertion" is usually wrong in practice: finding the insertion point is already a traversal, and moving contiguous memory is very fast (when to use a linked list).
- The same effect shows up in Java as arrays of objects versus arrays of primitives (Volume 3's lab).
Recall
Why can't the CPU overlap the loads of a linked-list walk?
Each load's address comes from the previous load, so the next can't start until the current one returns.
About how much slower is a main-memory access than an L1 hit on the lab machine?
About 100 times (roughly 1.6 ns against 100–165 ns).