Search code examples
binaryequalsoperation

Costs of binary operations


I've ask myself if its cheaper to use >= than ==.
Besides, that >= would be more secure in my case, which is a loop, adding objects to a list. The list itself always increase just by one, so a == would trigger.

It, for sure, does not affect any serious performance, but I just wanted to know.

Edit: The language would be Java, but I thought it were kinda universal.


Solution

  • At least for basic types, it wouldn't be any more costly. Essentially they would all call the same compare function, say C(a, b), and process the return -1, 0 or +1. For example >= would translate into C(a, b) != -1, == as !C(a, b), etc.

    In some languages though, you can reuse operators, and it might be that == vs. >= have significantly different costs.

    In cases where it doesn't matter between == and >=, use your personal preference, but keep it consistent. It can be jarring when maintaining code to see inconsistencies like these.