Optimizing a branch for a known more-common path

前端 未结 4 1104
深忆病人
深忆病人 2020-12-06 07:55

Please consider the following piece of code:

void error_handling();
bool method_impl();

bool method()
{
    const bool res = method_impl();
    if (res == f         


        
4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-06 08:01

    The underlying hardware already performs this optimizations. It will "fail" to predict it the first times, but after it will hit the correct option en.wikipedia.org/wiki/Branch_predictor.

    You can try applying the GCC extension and check if it is faster with it or not, but I think you will barely see any difference with it and without it. The branch prediction is applied always, it is not something you enable

提交回复
热议问题