Why Array Traversal is 10x FASTER Than Linked Lists
π― The Questionβ
"Both traversing an Array and traversing a Linked List have an linear time complexity. Why is iterating over a 10-million element Array 10x to 50x faster than iterating over a Linked List on modern CPUs?"
β‘ 30-Second Elevator Pitchβ
Modern CPU registers compute in 0.5 nanoseconds, but fetching data from physical RAM takes 50 to 100 nanoseconds. To bridge this speed gap, CPUs use ultra-fast L1/L2/L3 Hardware Caches.
- Array (Spatial Locality & Hardware Prefetching):
Arrays are contiguous blocks of RAM. When the CPU reads
arr[0], the hardware prefetcher loads an entire 64-byte Cache Line containingarr[0]througharr[15]. The next 15 iterations hit L1 Cache () with 0% RAM latency. - Linked List (Pointer Chasing & Cache Misses):
Linked list nodes are allocated independently across the Heap. Traversing to
node->nextrequires dereferencing random memory pointers, triggering a CPU Cache Miss on almost every single hop and stalling the CPU pipeline.
π§ Under-the-Hood: 64-Byte Cache Lines vs. Heap Pointer Chasingβ
π¬ Memory Overhead: Payload vs. Pointer Wasteβ
Consider storing 32-bit integers (int = 4 bytes):
- Array of 10M integers: of compact contiguous RAM.
- Linked List of 10M integers (64-bit):
Each node needs 4 bytes (data) + 4 bytes (alignment padding) + 8 bytes (
nextpointer) + 16 bytes (malloc allocator chunk header) = 32 bytes per node. Total = ( memory bloat!), consuming valuable cache space.
π Comparison Matrix: Array vs. Linked List Traversalβ
| Dimension | Contiguous Array | Singly Linked List |
|---|---|---|
| Theoretical Time Complexity | Linear Scan | Linear Scan |
| Actual Hardware Runtime | β‘ Blazing fast (~1β5 ms for 1M items) | π’ Slow (~50β100 ms for 1M items) |
| Spatial Locality | β Perfect (Consecutive addresses) | None (Scattered across heap) |
| Hardware Prefetcher | β 100% Effective (L1 Cache hits) | β Ineffective (Cannot predict next pointer) |
| Memory per Integer | 4 bytes | 24β32 bytes ( memory bloat) |
π‘ What Interviewers Ask Next (Follow-Up Traps)β
-
"What is Bjarne Stroustrup's famous benchmark on
std::vectorvsstd::list?"- Answer: Bjarne Stroustrup demonstrated that even when inserting elements into random sorted positions (where
std::listis theoretically after finding position andstd::vectoris due to shifting),std::vectorbeatsstd::listby large margins for moderate because cache locality and contiguous block memory copying (memmove) massively outperform pointer chasing.
- Answer: Bjarne Stroustrup demonstrated that even when inserting elements into random sorted positions (where
-
"What is an Unrolled Linked List?"
- Answer: An Unrolled Linked List is a hybrid structure where each linked list node contains a small array of 16β64 elements instead of 1 element. This combines the node insertion benefits of linked lists with the cache locality of arrays.
Interview Answer: Arrays traverse 10x faster than linked lists because contiguous memory enables CPU hardware prefetchers to load entire 64-byte cache lines into L1 cache ahead of time. Linked lists suffer from pointer chasing across scattered heap addresses, resulting in continuous cache misses.