Benefits of using short-circuit evaluation

后端 未结 15 1513
余生分开走
余生分开走 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:55

    Short circuit evaluation is translated into branches in assembly language in the same way if statements are (branches are basically a goto), which means it is not going to be any slower than if statements.

    Branches don't typically stall the pipeline, but the processor will guess whether the branch is taken or not, and if the processor is wrong it will have to flush everything that has happened since it made the wrong guess from the pipeline.

    Short circuit evaluation is also the most common name for it, and is found in most languages in some form or another.

提交回复
热议问题