Benefits of using short-circuit evaluation

后端 未结 15 1452
余生分开走
余生分开走 2020-12-05 16:23
boolean a = false, b = true;
if ( a && b ) { ... };

In most languages, b will not get evaluated because a is fals

相关标签:
15条回答
  • 2020-12-05 16:33

    First, your friend is wrong. Short-circuit evaluation (aka minimal evaluation) is available in most languages and is better than nested ifs for parallel languages (in which case the first of the conditions that returns will cause execution to continue)

    In any case, even in a straightforward non-parallel language I don't see how nested ifs would be faster as execution would block until the first condition is evaluated.

    0 讨论(0)
  • 2020-12-05 16:33

    How can a nested if not stall? Actually if a and b are both variables and not expressions with side effects, they can be loaded in parallel by a good compiler. There's no benefit to using more ifs except increasing your line count. Really, this would be the worst kind of second guessing a compiler.

    It's called short-circuit evaluation.

    0 讨论(0)
  • 2020-12-05 16:33

    I don't know anything about pipelining, but short-circuit evaluation is a common feature of many languages (and that's also the name I know it by). In C, && and the other operators define a sequence point just like the ; operator does, so I don't see how short-circuit evaluation is any less efficient than just using multiple statements.

    0 讨论(0)
  • 2020-12-05 16:34

    Regarding whether or not short-circuiting is efficient, pipelining is unlikely to have much of an impact on performance. In situations where it could have an impact, there's nothing to stop the compiler from testing multiple conditions in parallel so long as these tests doesn't have side-effects. Plus, modern CPUs have several mechanisms useful for improving the pipeline performance of branchy code.

    Nested ifs will have the same effect as short-circuiting &&.

    'Short-circuit evaluation' is the most common name for it, and your friend is wrong about it being uncommon; it's very common.

    0 讨论(0)
  • 2020-12-05 16:42

    Short-circuiting boolean expressions are exactly equivalent to some set of nested ifs, so are as efficient as that would be.

    If b doesn't have side-effects, it can still be executed in parallel with a (for any value of "in parallel", including pipelining).

    If b has side effects which the CPU architecture can't cancel when branch prediction fails then yes, this might require delays which wouldn't be there if both sides were always evaluated. So it's something to look at if you do ever find that short-circuiting operators are creating a performance bottleneck in your code, but not worth worrying about otherwise.

    But short-circuiting is used for control flow as much as to save unnecessary work. It's common among languages I've used, for example the Perl idiom:

    open($filename) or die("couldn't open file");
    

    the shell idiom:

    do_something || echo "that failed"
    

    or the C/C++/Java/etc idiom:

    if ((obj != 0) && (obj->ready)) { do_something; } // not -> in Java of course.
    

    In all these cases you need short-circuiting, so that the RHS is only evaluated if the LHS dictates that it should be. In such cases there's no point comparing performance with alternative code that's wrong!

    0 讨论(0)
  • 2020-12-05 16:44

    i've only heard of it as short circuit. in a pipeline doesnt the next op that goes in depend on the outcome of the if statement? if that is the case this would more optimized so it wouldnt have to test two values every time.

    0 讨论(0)
提交回复
热议问题