I am having a huge dilema right now. Consider this short code:
int* ptr = malloc(/* not important */);
if(ptr == NULL) {
// maybe free memory, do something, maybe exit
}
// do something else
Does branch prediction work in this example? Or, is it more limited from, say:
int* ptr = malloc(/* not important */);
if(ptr == NULL) {
// maybe free memory, do something, maybe exit
} else {
// do something else
}
...?
What I think is happening is that in the second example branch prediction can be utilised further by not loading code in the unlikely version of the condition, while the first example lacks this ability (or perhaps can only not load the code inside the if statement, but has to load the code outside of it). I need a confirmation / correction, please.
In case it is like so, should I pay more attention to this? I have just noticed I am sometimes writing single-branched conditions without giving it much thought (even though easily they could be multi-branched), now is when I realise perhaps multi branching might be better for my code.
Thanks.
Aucun commentaire:
Enregistrer un commentaire