Lesson 1 of 9 · 18m
Your Code Runs on Physics
The CPU/memory gap explains most counterintuitive performance behavior — learn the latency numbers before reaching for clever algorithms.
The first performance bug I ever owned was a tight parser loop that “optimized” a simple array scan into a pointer-chasing structure because pointers sounded sophisticated. It got ten times slower. Nothing in the code looked wrong; the mistake was treating memory as uniform. Memory is not uniform, and modern CPUs are fast precisely because they spend most of their silicon hiding that fact.
The uncomfortable latency ladder
A rough but useful mental table for one core on a modern server CPU:
L1 cache hit ~1 ns a few cycles
L2 cache hit ~3-5 ns about 10-20 cycles
L3 cache hit ~10-20 ns tens of cycles
DRAM access ~60-100 ns hundreds of cycles
SSD read tens of microseconds
Network round trip hundreds of microseconds to millisecondsExact numbers vary, but the ratios matter more than the digits. If one memory access misses every cache and goes to DRAM, the CPU could have executed hundreds of simple instructions in that time. That is why “fewer instructions” is often the wrong goal. Sometimes extra arithmetic is cheaper than one unpredictable memory access.
CPUs are not waiting politely
Your program does not execute one neat instruction at a time. A modern core is pipelined, superscalar, and out-of-order: it fetches many instructions, decodes several per cycle, renames registers, executes independent operations in parallel, and retires results in program order. The hardware preserves the illusion of serial execution while doing a controlled amount of chaos underneath.
int total = 0;
for (size_t i = 0; i < n; i++) {
total += values[i]; // simple, predictable, cache-friendly
}This loop is boring in exactly the right ways. The addresses are sequential, the loop direction is predictable, and the dependency chain is short enough that the core can overlap work. Compare it with code that jumps through object pointers or branches differently on every element.
The central trade: bandwidth versus latency
There are two separate problems:
- Latency is how long one access takes.
- Bandwidth is how much data can move per second when many accesses are in flight.
Caches and prefetchers exploit regularity. They are brilliant when you consume memory in streams and terrible when you consume it as random dependent pointers. Program performance often hinges less on what the code “does” and more on whether the machine can predict what memory it will touch next.
Field notes
- A cache miss is not an abstraction failure; it is a normal event with a measurable cost.
- Replacing arithmetic with table lookups can be a regression when the table no longer fits in cache.
- Big-O notation ignores constants that hardware magnifies: locality, dependencies, and prediction.
- Learn rough latency orders of magnitude. You do not need exact numbers to make good design decisions.
- Fast code usually looks plain: fewer layers, fewer pointers, tighter loops, clearer ownership.
Was this lesson helpful?