比较运算符性能(>,> =,<< =) [英] Comparison operator performance (>, >=, <, <=)
问题描述
如果您要比较两个整数,运算符会对执行比较所需的时间有影响吗?例如,给定:
If you were to compare two integers, would the operator have an impact on the time required to perform the comparison? For example, given:
if (x < 60)
和
if (x <= 59)
这将提供最佳性能,还是性能差异可以忽略不计?性能结果是否取决于语言?
Which would provide the best performance, or would the performance difference be negligible? Are the performance results language-dependent?
我经常发现自己在我的代码中使用这些操作符。任何想法都会感激。
I often find myself mixing the use of these operators within my code. Any thoughts would be appreciated.
推荐答案
即使有明显的区别,我认为编译器足够聪明地处理这些事情。所以我的建议是使用使代码更容易理解,并将微优化留给编译器。
Even if there was noticeable difference, I think compilers are smart enough to care for such things. So my advice is to use what makes the code easier to understand, and leave micro-optimizations to the compiler.
这篇关于比较运算符性能(>,> =,<< =)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!