使用短路计算的优点 [英] Benefits of using short-circuit evaluation

查看:130
本文介绍了使用短路计算的优点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

boolean a = false, b = true;
if ( a && b ) { ... };

在大多数语言中, B 将不会得到评估,因为 A 为false,所以 A &功放;&安培; b 不可能是真的。我的问题是,不会短路在建筑方面慢?在管道中,你只是拖延而等待获得的结果,以确定是否乙方应进行评估或不?它会更好做嵌套IFS呢?这是否甚至帮助?

In most languages, b will not get evaluated because a is false so a && b cannot be true. My question is, wouldn't short circuiting be slower in terms of architecture? In a pipeline, do you just stall while waiting to get the result of a to determine if b should be evaluated or not? Would it be better to do nested ifs instead? Does that even help?

此外,没有人知道什么是短路的评价通常是叫什么名字?这个问题出现后,我发现我的编程的朋友从来没有听说过短路评价,并指出,这是不常见的,也没有发现在许多语言,并在管道效率低下。我不能肯定最后一个的,所以问你的乡亲!

Also, does anyone know what short-circuit evaluation is typically called? This question arose after I found out that my programming friend had never heard of short-circuit evaluation and stated that it is not common, nor found in many languages, and is inefficient in pipeline. I am not certain about the last one, so asking you folks!

好吧,我觉得不同的例子或许可以解释在我的朋友可能会来自哪里。他认为,由于评估如下语句并行如下:

Okay, I think a different example to perhaps explain where my friend might be coming from. He believes that since evaluating a statement like the following in parallel:

(a) if ( ( a != null ) && ( a.equals(b) ) ) { ... }

将导致系统崩溃,不具有短路(从而不容许像上面的语句)会更快处理报表这样的一个架构:

will crash the system, an architecture that doesn't have short-circuiting (and thereby not allowing statements like the above) would be faster in processing statements like these:

(b) if ( ( a == 4 ) && ( b == 5 ) )

,因为如果它不能并行执行(a)中,它不能并行执行(b)中。在这种情况下,允许短路的语言慢于一个不

since if it couldn't do (a) in parallel, it can't do (b) in parallel. In this case, a language that allows short-circuiting is slower than one that does not.

我不知道这是真的还是假的。

I don't know if that's true or not.

感谢

推荐答案

的短路布尔前pressions是完全等效于某些组嵌套IFS的,所以都是同样有效,这将是。

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

如果B没有副作用,它仍可以并行执行与一个(在并联,包括流水线中的任何值)。

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

如果B有副作用时,分支prediction失败,那么是的,这可能需要延迟,并不会在那儿,如果双方总是被评估CPU架构无法取消。所以它的东西来看待,如果你曾经发现短路运营商都建立在你的code性能瓶颈,但不值得担心其他。

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.

但短路用于尽可能多的控制流程,以节省不必要的工作。这是我使用的语言中常见,例如Perl的成语:

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");

外壳成语:

do_something || echo "that failed"

或C / C ++ / Java的/等成语:

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

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

在所有这些情况下,需要的短路,因此,如果在LHS决定,它应该是在RHS仅评估。在这种情况下,有没有比较有替代code性能上来看这是错误的!

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!

这篇关于使用短路计算的优点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆