使用逻辑运算符评估条件的多个If语句与单个语句

时间:2020-02-23 15:27:31

标签: c++ performance if-statement logical-operators

我正在编写一个在实际执行其任务之前检查几种条件的函数。这是通过许多if语句完成的。像这样:

bool foo()
{
    if(invalid())
        return false;
    if(dont_execute())
        return false;
    // .. etc
    // Actual execution here
    return true;
}

在此功能中,将多个条件更改为:

bool foo()
{
    if(invalid() || dont_execute() /* || .. etc */)
        return false;
    // Actual execution here
    return true;
}

我认为第一种样式更具可读性。我想知道的是,使用多个if语句而不是使用逻辑运算符进行组合是否会对性能产生影响。

2 个答案:

答案 0 :(得分:5)

没有任何性能影响。如果我们compare the assembly两个函数都可以看到,则两个函数都相同。

示例:

save.image

这里是程序集:

bool f1();
bool f2();

bool combined()
{
    if (f1() || f2())
        return false;

    return true;
}

bool separate()
{
    if (f1())
        return false;

    if (f2())
        return false;

    return true;
}

答案 1 :(得分:2)

使用测试用例

bool invalid();

bool dont_execute();

void execute();

bool foo()
{
    if(invalid())
        return false;
    if(dont_execute())
        return false;
    execute();
    return true;
}

bool foo2()
{
    if(invalid() || dont_execute() /* || .. etc */)
        return false;
    execute();
    return true;
}

您会看到foofoo2都被带有-O2优化标志的GCC 9.2和Clang 9编译为完全相同的程序集,请参见godbolt。例如,GCC的输出是

foo():
        sub     rsp, 8
        call    invalid()
        test    al, al
        je      .L2
.L4:
        xor     eax, eax
        add     rsp, 8
        ret
.L2:
        call    dont_execute()
        test    al, al
        jne     .L4
        call    execute()
        mov     eax, 1
        add     rsp, 8
        ret
foo2():
        sub     rsp, 8
        call    invalid()
        test    al, al
        je      .L8
.L10:
        xor     eax, eax
        add     rsp, 8
        ret
.L8:
        call    dont_execute()
        test    al, al
        jne     .L10
        call    execute()
        mov     eax, 1
        add     rsp, 8
        ret

虽然这并不意味着不会出现差异,但至少编译器认为这两种情况下他们不需要做任何不同的事情,即使他们不知道什么这些功能叫做do。

因此,我建议您不要担心性能,但请考虑您认为可读性更高的内容。