Why reading an array by index is instant
Reading xs[5] takes the same time whether the array holds ten items or ten million. Here is the small sum the computer does to get there, drawn one move at a time.
01 The question
You have an array of a million numbers. Reading the first one and reading the last one cost the same. Nothing is searched and nothing is counted, so how does the computer know where item 999,999 is without walking past the others?
02 The naive picture: walk along
It is easy to imagine the computer starting at item 0 and stepping forward five times to reach xs[5]. That is how a linked list works: each node only knows where the next one is, so reaching item i means following i links.
An array does not need to walk, because of one rule about how it is laid out in memory.
03 Memory is one long row of numbered bytes
Memory is a long row of bytes, and every byte has a number: its address. When you make an array, the program reserves one unbroken stretch of that row and puts the items in it side by side, in order, with no gaps. This is what contiguous means.
Every item has the same size. In C an int is typically 4 bytes, so an array of 8 ints that starts at address 1000 takes bytes 1000 to 1031, and each item begins 4 bytes after the one before.
| Index | Value | Starts at |
|---|---|---|
| 0 | 7 | 1000 |
| 1 | 3 | 1004 |
| 2 | 9 | 1008 |
| 3 | 4 | 1012 |
| 4 | 8 | 1016 |
| 5 | 2 | 1020 |
| 6 | 6 | 1024 |
| 7 | 5 | 1028 |
04 What really happens: one multiply, one add
Because the items are the same size and packed together, the address of item i is a formula: start + i × size. The CPU does this sum, then reads memory once. In machine code it is often a single load instruction with the multiply and add built in.
- start address
- computed address
- the item read
- just worked out
The program asks for xs[5]. The CPU knows two facts about xs: where it starts and how many bytes each item takes. Nothing has been read yet.
The formula also explains why most languages count from 0. The first item is at start + 0 × size, which is the start itself, so its index is 0.
| Read | Offset (i × 4) | Address | Work |
|---|---|---|---|
| xs[0] | 0 | 1000 | 1 multiply, 1 add, 1 read |
| xs[5] | 20 | 1020 | 1 multiply, 1 add, 1 read |
| xs[999999] | 3999996 | 4000996 | 1 multiply, 1 add, 1 read |
05 See the addresses yourself
C lets you print where each item lives. Compile this with any C compiler and run it.
Your addresses will differ from run to run. The gaps will not: each address is 4 more than the last (in hex, c0, c4, c8, cc, d0, d4).
06 What about lists of mixed things?
A Python list can hold 7, "hello" and 3.5 side by side, and those are not the same size. In CPython the list does not store the objects themselves. It stores a contiguous array of pointers to them, and on a 64-bit machine every pointer is 8 bytes. So xs[i] uses the same formula to find the pointer, then follows it to the object.
Java object arrays and JavaScript arrays of mixed values work in a similar way: the slots are fixed-size references.
The rule that makes indexing instant is same-size slots, side by side. Whatever a language puts in the slots, numbers or pointers, the address of slot i is still start + i × size.
07 When it is not instant
9 in xsSearching by valueReading by index is instant. Finding which index holds a value is not: the array has to check items one by one until it finds a match.xs.insert(0, v)Inserting at the frontThe items must stay packed in order, so every item after the new one shifts one slot along. On a million items, that is a million moves.list.get(500)A list that is not an arrayJava’s LinkedList has the same get(i) method as ArrayList, but it walks node by node to get there. Same name, very different cost.xs[8]Reading past the endIn C the sum still runs and the CPU reads whatever sits at that address. Python and Java check the index against the length first and raise IndexError or ArrayIndexOutOfBoundsException.