Comparison operator performance (>, >=, <, <=)

后端 未结 4 1359
鱼传尺愫
鱼传尺愫 2020-12-11 15:23

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)


        
相关标签:
4条回答
  • 2020-12-11 16:11

    There is almost certainly no difference in performance. For CISC processors, you'll typically have all manner of branch instructions that cope with all the difference < <= > >= etc. On RISC there may be a very small performance difference although I'd seriously doubt it!

    0 讨论(0)
  • 2020-12-11 16:12

    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.

    0 讨论(0)
  • 2020-12-11 16:13

    In the specific example you gave where one side is constant, I'd expect an optimizer to transform one to the other if it was significantly faster.

    0 讨论(0)
  • 2020-12-11 16:16

    The differences are negligible. Theoretically they could be language dependent.

    As another answer mentioned, they are also theoretically platform dependent.

    See: Is the inequality operator faster than the equality operator?

    0 讨论(0)
提交回复
热议问题