Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)

and

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.

like image 431
Evan Mulawski Avatar asked May 02 '11 18:05

Evan Mulawski


People also ask

IS => A comparison operator?

Comparison operators — operators that compare values and return true or false . The operators include: > , < , >= , <= , === , and !== . Logical operators — operators that combine multiple boolean expressions or values and provide a single boolean output.

What is the meaning of this comparison operator <>?

What Does Comparison Operator Mean? In C#, a comparison operator is a binary operator that takes two operands whose values are being compared. Comparison operators are used in conditional statements, especially in loops, where the result of the comparison decides whether execution should proceed.

What does the === comparison operator do?

The strict equality operator ( === ) checks whether its two operands are equal, returning a Boolean result. Unlike the equality operator, the strict equality operator always considers operands of different types to be different.

Which one is faster == or ===?

So === faster than == in Javascript === compares if the values and the types are the same. == compares if the values are the same, but it also does type conversions in the comparison. Those type conversions make == slower than ===.


1 Answers

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.

like image 56
Alexey Kukanov Avatar answered Sep 29 '22 06:09

Alexey Kukanov