0Pricing
C++ Academy · Lesson

Branch Prediction and Hot Loops

Help the CPU predict branches and write loops the compiler can optimize.

Branch Prediction and Hot Loops is a free C++ Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

The Branch Predictor

Modern CPUs predict the next instruction before the previous one finishes. When the prediction is wrong, the pipeline stalls — costing 10-20 cycles.

Predictable Branches

Branches that almost always go one way (or follow a clear pattern) are well predicted. Random branches are catastrophic.

Sorted Data Helps

Iterating sorted data with a conditional is often faster than unsorted — the predictor learns the pattern.

// Often much faster on sorted data
std::sort(v.begin(), v.end());
int sum = 0;
for (int x : v) {
    if (x > 128) sum += x;
}

Branchless Code

Replace branches with arithmetic when possible. The CPU evaluates both paths and selects with no branching.

// Branchy
int max(int a, int b) { return (a > b) ? a : b; }

// Branchless (often equivalent in machine code)
int max(int a, int b) { return a + (b - a) * (b > a); }

std::max as the Better Choice

Modern compilers often produce branchless code automatically. Just write clear code and let the optimizer work — but inspect the disassembly on hot paths.

Likely and Unlikely Hints

C++20 added [[likely]] and [[unlikely]] to give the compiler a hint.

if (error_condition) [[unlikely]] {
    handle_error();
}

Loop Unrolling

Doing more work per iteration reduces branch frequency. Modern compilers unroll on their own; use #pragma unroll only when measured to help.

Avoid Mixed Workloads in Loops

A loop with two cases that alternate randomly defeats prediction. Split into two loops (one per case) when possible.

// Slow: random switching
for (auto& x : v) {
    if (x.type == A) process_A(x);
    else            process_B(x);
}

// Faster: partition first
std::partition(v.begin(), v.end(), [](auto& x) { return x.type == A; });
for (size_t i = 0; i < boundary; ++i) process_A(v[i]);
for (size_t i = boundary; i < v.size(); ++i) process_B(v[i]);

Inline Hot Functions

Function call overhead can rival the function body in hot loops. inline hints help; __attribute__((always_inline)) (GCC/Clang) is stricter.

Don t Trust Intuition

Compilers and CPUs are very smart. Measure before optimizing. The "obvious" branchless version may be slower than the branchy one once the predictor is trained.

SIMD for Wide Loops

Auto-vectorization turns a loop into SIMD instructions when it can. Use clean code, simple types, and avoid data dependencies between iterations.

Profile-Guided Optimization (PGO)

Compile, run, collect profile data, recompile with hints. Compilers use the data to make better predictions about which branches are hot.

Quick Check

Why might sorting data before a filtering loop sometimes speed up execution?

Recap

Mispredicted branches cost cycles. Keep branches predictable (sorted data helps), use likely/unlikely hints, partition workloads to keep loops uniform, and trust the compiler to optimize unless profiling proves otherwise.

Frequently asked questions

Is the “Branch Prediction and Hot Loops” lesson free?

Yes — the full text of “Branch Prediction and Hot Loops” is free to read here on the web, and the C++ Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C++ Academy course, upgrade to CoddyKit PRO.

What will I learn in “Branch Prediction and Hot Loops”?

Help the CPU predict branches and write loops the compiler can optimize. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C++ Academy?

No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Branch Prediction and Hot Loops” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C++ Academy lesson?

Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Cache-Friendly Data Layouts
  2. Branch Prediction and Hot Loops
  3. Profiling with perf vtune and Sanitizers
  4. Micro-benchmarking with Google Benchmark
← Back to C++ Academy