[C++] TIL that you can use likely() and unlikely() to help the compiler with branch prediction

So if you’re running a null check, but you’re pretty sure it’ll be false, you can say

if(unlikely(ptr == null)){
    doStuff()
}

Of course, the performance increase here is small, but if you’re working on performance critical code, this is pretty useful

Further reading

Submitted June 19, 2016 at 11:52AM by novinicus
via reddit https://www.reddit.com/r/ProgrammerTIL/comments/4oulcn/c_til_that_you_can_use_likely_and_unlikely_to/?utm_source=ifttt

Categories: Uncategorized

Leave a Reply