因为如果条件更好的算法算法、条件

由网友(一夕笙歌)分享简介:我有一个code,其中包含I am having a code, which containsbool fn(){...//all the following are boolean functions.return isTrue()&&isMsgReceived()&&isMsgSent();}的问题是...

我有一个code,其中包含

I am having a code, which contains

bool fn()
{
...
//all the following are boolean functions.
return isTrue()
       &&isMsgReceived()
       &&isMsgSent();
}

的问题是,每一个返回布尔函数本身是相当冗长和需要大量的计算。其实,有在随后的检查功能,如果previous 1已经失败(状态)没有任何意义。

The problem is that each of the return boolean functions are themselves quite lengthy and takes much computation. Actually, there is no point in checking subsequent functions, if previous one already failed (and condition).

您会建议更简单的方式返回,在案件的开始功能之一已经失败,而不是去作进一步检查。这样做的目的是减少计算时间。

Would you suggest simpler ways to return false, in case one of the beginning functions already failed and not go for further checking. The aim is to reduce compute time.

推荐答案

&功放;&安培; 已不适合您。如果 IsTrue运算()返回,接下来的两个函数甚至没有进行评估。

&& already does that for you. If isTrue() returns false, the next two functions are not even evaluated.

因此​​,它是有道理的,首先把最便宜的函数链中的&功放;&安培; 取值

It therefore makes sense to put the least expensive function first in a chain of &&s.

阅读全文

相关推荐

最新文章