Search code examples
c++profilingbranch-prediction

Measure how often a branch is mispredicted


Assuming I have a if-else branch in C++ how can I (in-code) measure how often the branch is mispredicted? I would like to add some calls or macros around the branch (similar to how you do bottom-up profiling) that would report branch mispredictions.

It would be nice to have a generic method, but lets do Intel i5 2500k for starters.


Solution

  • If you are using an AMD CPU, AMD's CodeAnalyst is just what you need (works on windows and Linux)*.

    if your not, then you may need to fork out for a VTune licence or build something using the on CPU performance registers and counters details in the instruction manuals.

    You can also check out gperf & OProfile (linux only), see how well they perform (I've never used these, but I see them referred to quite a bit).

    *CodeAnalyst should work on an Intel CPU, you just don't get all then nice CPU level analysis.