Skip to content

Why reading an array by index is instant

BasicsUnder the hood

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?

Python
xs = [7, 3, 9, 4, 8, 2, 6, 5]
xs[0] # 7
xs[5] # 2, just as quick

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.

Linked list, item ii hopsEach node says where the next one is. Item 1,000 is a thousand pointer reads away.
Array, item i1 multiply, 1 addThe address is worked out directly, so item 1,000 costs the same as item 1.

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.

Where each item of xs starts (a toy start address of 1000)
IndexValueStarts at
071000
131004
291008
341012
481016
521020
661024
751028

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.

Reading xs[5]
Step 1 of 5
Item size 4 bytesMemory reads 0
xs · 8 ints of 4 bytes, from address 1000
70
31
92
43
84
25
66
57
The sum the CPU does
Empty
  • 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.

5 index

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.

The work does not grow with the index
ReadOffset (i × 4)AddressWork
xs[0]010001 multiply, 1 add, 1 read
xs[5]2010201 multiply, 1 add, 1 read
xs[999999]399999640009961 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.

addr.c
#include <stdio.h>
int main(void) {
int xs[6] = {7, 3, 9, 4, 8, 2};
printf("sizeof(int) = %zu\n", sizeof(int));
for (int i = 0; i < 6; i++)
printf("xs[%d] = %d at %p\n", i, xs[i], (void *)&xs[i]);
return 0;
}
Shell
$ cc addr.c -o addr && ./addr
sizeof(int) = 4
xs[0] = 7 at 0x16aebe8c0
xs[1] = 3 at 0x16aebe8c4
xs[2] = 9 at 0x16aebe8c8
xs[3] = 4 at 0x16aebe8cc
xs[4] = 8 at 0x16aebe8d0
xs[5] = 2 at 0x16aebe8d4

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.

A CPython list: same-size pointers in a row, objects stored elsewhere
The list · 8-byte pointers in a row
→ 70
→ "hello"1
→ 3.52
The objects, wherever they were made
3.5
7
"hello"

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.
Same steps, not always the same speedThe arithmetic never changes, but the memory read can. A read the CPU cache can serve is typically far faster than one from main memory, often quoted as around 1 ns against 100 ns. Reading items in order is kind to the cache; jumping around at random is not.

08 Check yourself

Question

Array a has 1,000 items and array b has 1,000,000. Which read does less work: a[500] or b[500]?

Question

An array of 8-byte numbers starts at address 2000. Where does item 3 start?

Question

Why can’t a linked list use the same sum to reach its 5th node?

09 Practise this

Was this tutorial useful?Your vote tells us what to improve next.
Spotted a mistake, or something unclear?