2 ms·
Your point about branch prediction working better in a microbenchmark than in real code is true in some scenarios. But it's also the case that if the code is ca
by eklitzke 4y ago
Your point about branch prediction working better in a microbenchmark than in real code is true in some scenarios. But it's also the case that if the code is called infrequently enough in the real program that it doesn't get good branch prediction it may not be worth optimizing, since that may mean it's not actually on a hot path.
There are also plenty of examples of microbenchmarks you can write for this type of code that don't suffer from the problem of artificially high branch prediction. An example used in this article is a function called downHeap which is presumably used as part of heap sort. Searching and sorting are good examples of algorithms that often don't do well with branch prediction. As a simple example, if you're doing a binary search for a random element in a sorted vector you'll expect half the branches to go one way (left of the pivot for the iteration) and half the branches to go the other way (right of the pivot for the iteration) without a predictable pattern, even in a microbenchmark.