Speed of Comparison operators

后端 未结 6 2273
走了就别回头了
走了就别回头了 2020-12-19 08:45

In languages such as... well anything, both operators for < and <= (and their opposites) exist. Which would be faster, and how are they interpreted?

6条回答
  •  心在旅途
    2020-12-19 09:48

    Leaving this as vague as you have has caused this to be an unanswerable question. Performance cannot be evaluated unless you have software and hardware to measure - what language? what language implementation? what target CPU architecture? etc.

    That being said, both <= and < are often identical performance-wise, because they are logically equivalent to > and >=, just with swapped destinations for the underlying goto's (branch instructions), or swapped logic for the underlying "true/false" evaluation.

    If you're programming in C or C++, the compiler may be able to figure out what you're doing, and swap in the faster alternative, anyway.

    Write code that is understandable, maintainable, correct, and performant, in that order. For performance, find tools to measure the performance of your whole program, and spend your time wisely. Optimize bottlenecks only until your program is fast enough. Spend the time you save by making better code, or making more cool features :)

提交回复
热议问题