简体   繁体   English

if语句是否带有否定

[英]if statement with or without negation

Obviously, these two samples achieve the same thing. 显然,这两个示例实现了相同的目的。 But are there any, perhaps implementation-specific cases, where one could have better performance than the other? 但是,在任何情况下,也许有特定于实现的情况,其中一种可能比另一种具有更好的性能?

Maybe a silly question, but this has got me thinking today. 也许这是一个愚蠢的问题,但这使我今天思考。

Edit: The example uses strings but this can be anything , and my question has nothing to do with how fast a string comparison is etc. 编辑:该示例使用字符串,但这可以是任何东西 ,我的问题与字符串比较的速度等无关。

if (something == 'something') {
  return "something's up";
}
return;

vs

if (something != 'something') {
  return;
}
return "something's up";

One could out perform the other very slightly if you anticipate how the if will trigger on average and how your specific CPU handles branch prediction . 如果您预计if将如何平均触发以及您的特定CPU如何处理分支预测 ,则一个人可能会在执行其他任务时表现非常差。 For instance if you know your CPU will always predict that the branch will lead into the if statement, and you expect something to equal true more often than not, then the first choice: 例如,如果您知道您的CPU将始终预测该分支将进入if语句,并且您希望某事经常等于真,那么第一种选择是:

if (something == true) {
  return true;
}
return;

will out perform the second. 将执行第二。 CPU branch predictors are rarely this simple and are adaptive now ( http://en.wikipedia.org/wiki/Branch_predictor see "saturating counter") but hopefully this gives you a little insight. CPU分支预测器很少这么简单,现在可以自适应( http://en.wikipedia.org/wiki/Branch_predictor参见“饱和计数器”),但希望这能给您一些启示。 Either way the performance increase is extremely slight, especially on today's systems. 无论哪种方式,性能提升都非常微小,尤其是在当今的系统上。

:ADDITION: :加成:

As for the two in the comments: When it reaches the CPU as assembly they will be exactly the same as what you have above so nothing really changes. 至于注释中的两个:当它以组装的形式到达CPU时,它们将与您上面的内容完全相同,因此实际上没有任何变化。

Either way it's fine, from a performance point of view they're almost certainly identical and any difference would be negligible. 无论哪种方式都很好,从性能的角度来看,它们几乎可以肯定是相同的,任何差异都可以忽略不计。 But for clarity, it's recommended that you write conditions like this: 但为清楚起见,建议您编写如下条件:

if (something) {  // no need to compare something == true
  return true;
}
return false;

Or even better: 甚至更好:

return something; // just return the boolean value!

No matter which version you choose, you should optimize for readability an clearness. 无论选择哪个版本,都应该针对可读性进行优化以提高清晰度。 It's more important to have a good name for the variable than the actual order in which you write the conditions. 给变量起一个好名字比写条件的实际顺序更重要。

Code is read and modified a lot more often than it's written. 与编写代码相比,读取和修改代码的频率更高。 Since performance is the same when translated into assembly language, if statements should be written so that's it is the easiest to read. 由于翻译成汇编语言时的性能是相同的,因此应该编写if语句,以便于阅读。

Example : 范例:

if (x > (2 + 3)) //or something more complex
{
    return true;
}
else
{
    return false;
}

is easier to read that way : 这样更容易阅读:

bool isGreaterThanFive = x < (2 + 3)

if (isGreaterThanFive)
{
    return true;
}
else
{
    return false;
}

and even more compact : 甚至更紧凑:

if (isGreaterThanFive) return true; //Very close to human language
else return false;

OP's first code block si easier to read so it should be written that way, performance not being an issue. OP的第一个代码块更易于阅读,因此应该以这种方式编写,而性能不是问题。

EDIT : more about code readability, Code Complete by Steve McConnell http://en.wikipedia.org/wiki/Code_Complete 编辑:有关代码可读性的更多信息,史蒂夫·麦康奈尔(Steve McConnell)的代码完成http://en.wikipedia.org/wiki/Code_Complete

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM