Comparison Lab

Dynamic array vs linked list

For the same sequence of push_back, push_front and pop_front, what does each structure have to do: move elements, allocate memory, or rewire pointers?

No wall-clock benchmarks: every number on this page is an operation count taken from the algorithms' traces, never a timing.

What is counted

Operations
One public operation call. Both sides run the same sequence, so this row is equal by construction; it is the denominator for the others.
Allocations
One block of memory obtained during the sequence: a new, larger buffer for the array (capacity 1, 2, 4, …), or a new node for the list. Both start empty with nothing allocated.
Elements moved
One already stored element copied into a different slot: copies into a new buffer while growing, plus one-slot shifts inside the buffer (push_front shifts right, pop_front shifts left).Linked list: not applicable. A list never moves a stored value: each stays in its node until it is removed. The list trace has no copy or shift events.
Array slot writes
One store into a buffer slot: the new element, every growth copy and shift, and clearing the vacated last slot on pop_front. Updates to the size field are not counted on either side.Linked list: not applicable. The list has no slots; each value is stored once, inside the node counted under Allocations.
Pointer writes
One assignment to a node's next field or to the list's head or tail reference.Dynamic array: not applicable. The array has no links: it reaches element i by address arithmetic.
  • Dynamic array: Starts with capacity 0 and doubles when full. push_front = insert(0, v); pop_front = removeAt(0).
  • Linked list: Head and tail references. push_back = append, push_front = prepend, pop_front = remove(front value), which stops at the head.

Totals for every preset

Computed when this page was built, by running each algorithm on the preset (seed 1). The lab above replays the same traces.

PresetMetricDynamic arrayLinked list
push_front × 12Operations1212
Allocations512
Elements moved81n/a
Array slot writes93n/a
Pointer writesn/a25
push_back × 12Operations1212
Allocations512
Elements moved15n/a
Array slot writes27n/a
Pointer writesn/a24
Used as a queueOperations1313
Allocations48
Elements moved28n/a
Array slot writes41n/a
Pointer writesn/a21
push_front × 12:
Every push_front shifts all stored elements one slot right in the array (0 + 1 + … + 11 = 66 shifts), plus growth copies. The list links one new node at the head each time.
push_back × 12:
Appending: the array only copies when it grows (1 + 2 + 4 + 8 = 15 copies, 5 buffers); the list allocates 12 nodes and writes 2 pointers per append.
Used as a queue:
push_back at one end, pop_front at the other. Each pop_front shifts every remaining element left in the array; in the list it moves head by one node.

What to look for

The two structures do different kinds of work, so the table never adds them up. The array moves elements; the list rewires pointers. Which is cheaper on real hardware depends on element size, allocation cost and caching, none of which these counts measure.

At the back, the array is cheap: only the occasional growth copies, which double the capacity each time so their total stays below 2n. At the front, every push_front and pop_front moves all stored elements.

The list does a constant amount of pointer work per push or pop at either end, but pays one allocation per element, where the array allocated a handful of buffers.

Each side on its own, with code in four languages: Dynamic array, Linked list.