Optimizing a branch for a known more-common path

前端 未结 4 1127
深忆病人
深忆病人 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

    Without knowing the implementation of std::time() I wouldn't conclude much from this test. From your own results, it seems like it dominates the time in the loop.

    FWIW, I use likely()/unlikely() liberally myself when tuning code. I do not want to change the structure of the code, but when reading the assembly, I'd like to see the common path be a straight line of untaken branches. The key here (for me) is the readability of the assembly. The fact that this is also what will be fastest is secondary (the fastest possible branch is a correctly predicted untaken branch).

提交回复
热议问题