Sub topic 5
Comparing the cost of each implementation
This chapter assumes you know Big-O notation for common operations and have seen the three queue implementations from earlier chapters: a list with pop(0), a linked list with head and tail pointers, and a circular buffer. Here you will put them side by side and learn how to measure the difference yourself.
The four operations that matter
A queue is used through four operations: enqueue (add at the back), dequeue (remove from the front), peek (read the front without removing it), and size. The cost of a whole program is usually dominated by enqueue and dequeue, because they run once per item.
| Implementation | Enqueue | Dequeue | Peek | Size | Capacity |
|---|---|---|---|---|---|
| List with `pop(0)` | O(1) amortized | O(n) | O(1) | O(1) | Grows |
| Linked nodes, head and tail | O(1) | O(1) | O(1) | O(1) with a counter | Grows |
| Circular buffer | O(1) | O(1) | O(1) | O(1) | Fixed, or O(n) to resize |
The list is the outlier. pop(0) removes the first element and then shifts every remaining element one slot left. A queue of 100,000 items pays 100,000 moves for one dequeue.
Where the constants hide
Two implementations with the same Big-O can still differ in speed.
- Linked nodes allocate one object per item. Each node also stores a pointer to the next node, so memory use per item is higher. Nodes can be scattered in memory, which is slower for the CPU cache than a contiguous array.
- Circular buffers store items in one array and only update two integers per operation. They are compact and cache-friendly. The price is a fixed capacity. A growable version must copy all items into a larger array when full. Like a list's
append, that copy is O(n) once but O(1) amortized across many enqueues. - Lists are fast at the back and slow at the front. If you only ever append and read, they are fine. The problem appears only when you dequeue from the front.
Measuring it yourself
Complexity claims are easiest to believe when you see them. This script drains a list and a deque of growing sizes and prints the time for each.
import timeit
from collections import deque
def drain_list(n):
items = list(range(n))
while items:
items.pop(0)
def drain_deque(n):
items = deque(range(n))
while items:
items.popleft()
for n in (10_000, 20_000, 40_000):
t_list = timeit.timeit(lambda: drain_list(n), number=3)
t_deque = timeit.timeit(lambda: drain_deque(n), number=3)
print(f"n={n:>6} list={t_list:.4f}s deque={t_deque:.4f}s")Exact times depend on your machine. The pattern should not. Each time n doubles, the deque time roughly doubles (n dequeues at O(1) each). The list time grows closer to four times, because n dequeues at O(n) each is O(n²) in total.
Reading the table for decisions
Use these rules when picking a structure:
- If the queue is always small (a few dozen items), the simplest option wins. A list is acceptable.
- If the queue can grow large and you dequeue often, avoid
pop(0). - If you know the maximum size and want predictable memory, use a circular buffer.
- In everyday Python code,
collections.dequegives you O(1) at both ends without writing your own class. The next chapters cover it in detail.
Common mistakes
- Counting only enqueue. Appending to a list is cheap, so a queue built on a list seems fast until the first dequeue-heavy run.
- Ignoring resize cost. A growable circular buffer has an occasional O(n) copy. This matters in real-time code where one slow operation is worse than a slightly slower average.
- Confusing amortized with worst case. Amortized O(1) means the average over many operations. A single operation can still be slow.
Exercise
A queue holds 50,000 items and you dequeue all of them using list.pop(0). Roughly how many element moves happen in total, and which implementation avoids this cost?
Show answer
Each dequeue shifts the remaining items, so the total is about 50,000 + 49,999 + ... + 1, which is roughly 50,000 × 50,000 / 2, or about 1.25 billion moves. This is O(n²) overall. A linked queue with head and tail pointers, a circular buffer, or collections.deque each dequeue in O(1), so the total is only 50,000 operations.