Branch Prediction: Sorted vs. Shuffled Data
Question 36 / 51 • Correct so far: 0 (0 answered)
Sorted Array
std::size_t filterAboveThreshold(const std::vector<int>& data,
std::vector<int>& out) {
std::size_t n = 0;
for (int x : data)
if (x >= kThreshold) out[n++] = x;
return n;
}
std::size_t n = filterAboveThreshold(SORTED_DATA, OUTPUT); Shuffled Array
std::size_t filterAboveThreshold(const std::vector<int>& data,
std::vector<int>& out) {
std::size_t n = 0;
for (int x : data)
if (x >= kThreshold) out[n++] = x;
return n;
}
std::size_t n = filterAboveThreshold(SHUFFLED_DATA, OUTPUT); Shared test data (shared-setup)
constexpr int kArraySize = 32768;
constexpr int kThreshold = 128;
static std::vector<int> generateData() {
std::mt19937 rng(42);
std::uniform_int_distribution<int> dist(0, 255);
std::vector<int> v(kArraySize);
for (auto& x : v) x = dist(rng);
return v;
}
static const std::vector<int> SHUFFLED_DATA = generateData();
static std::vector<int> makeSorted() {
auto v = generateData();
std::sort(v.begin(), v.end());
return v;
}
static const std::vector<int> SORTED_DATA = makeSorted();
// Pre-allocated output buffer reused across iterations to avoid allocation cost.
static std::vector<int> OUTPUT(kArraySize); Which snippet is faster?
Snippet A is faster. With sorted data the branch predictor learns the single transition point and rarely mispredicts; with shuffled data the outcome is essentially random, causing ~50% mispredictions throughout the loop. A conditional store cannot be folded into a branchless CMOV, so each misprediction flushes the pipeline and wastes approximately 15–20 cycles.
Benchmark results
| Snippet | CPU time / iteration | Speedup |
|---|---|---|
| Shuffled Array | 21.4 us | 1.0× |
| Sorted Array | 7.2 us | 3.0× |
Explore the source
Open in Compiler ExplorerQuiz complete. You can return to the question list to restart and compare.