Learn C Programming

Lesson 5 of 7 · Decisions

Module 5 · Decisions

Under the Hood: How a Branch Becomes a Jump

ProReading

Kenji added up the same 32768 numbers 2000 times, first shuffled, then sorted. With GCC's optimiser switched off, the shuffled run took 421 ms and the sorted run 159 ms. Same numbers, same additions, and the sorted order was about 2.6 times faster. Then he compiled the way the Playground does, and the gap disappeared.

Checking what this lesson needs for you…

What is inside Under the Hood: How a Branch Becomes a Jump

  1. A processor has no if, only jumps
  2. One if, compiled: a compare and a conditional jump
  3. The same kind of if with no jump at all: cmov
  4. A dense switch becomes a jump table
  5. Branch prediction: the processor guesses
  6. Kenji's loop, measured
  7. What this means for you today