Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Branch Misses

Name: Anonymous 2011-09-26 17:26

How bad do branch misses hurt a programs performance? In C and Java?

for example how big of an impact would :


int shitinanus()
{
    int val = getanus();
    if(val != 0)
        return ANALTONIGHT;
    return ALONETONIGHT;
}


be knowing that val will most likely be 0 99% of the time compared to the below function


int shitinanus()
{
    int val = getanus();
    if(val == 0)
        return ALONETONIGHT;
    return ANALTONIGHT;
}

Name: Anonymous 2011-09-26 18:46

Surely you could have taken the five minutes to actually benchmark that code. I suspect you just wanted to shit up /prog/ with your puerility.

Ah, well. With a bit of luck, you would find that in your example it doesn't make a lick of a difference, due to one of two factors:
1. Any modern CPU keeps a history buffer with taken branches, so no matter how you organize your code, it will predict a branch to the common case for frequently executed branches.
2. If you don't have a modern CPU, i.e., one with OoOE, your example should compile to a conditional move, eliminating the branch altogether.

To give some actual numbers: the branch misprediction penalty on a Cortex-A8 is 13 cycles, and on a Cortex-A9 it's 8 cycles.
I found some interesting data on other processors at http://www.7-cpu.com/ , though I can't vouch for its accuracy.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List