因为如果条件更好的算法 [英] Better algorithm for if condition
问题描述
我有一个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.
这篇关于因为如果条件更好的算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!