I wonder which operation works faster:
int c = version1.compareTo(version2);
This one
if (c == 1)
or this
if (c > 0)
Does sign comparasion use just a one bit check and equality comparasion use substraction, or it is not true? For certainty, let's say we work on x86.
P.S. Not an optimization issue, just wondering how it works.
Assuming those operations are JITted into x86 opcodes without any optimization, there is no difference. A possible x86 pseudo-assembly snippet for the two cases could be:
and:
The
cmp
operation performs a subtraction between the two operands (registeri
and immediate0
), discards the result and sets some flags: positive, negative, overflow etc.These flags are then used to trigger a conditional jump (i.e. jump if condition), in one case if the two operands are equal, in the second case if the first is greater than the second.
Again, this without considering any software (JVM-wise) and/or hardware optimization. In fact, x86_64 architectures have a complex pipeline with advanced branch-prediction and out-of-order execution, for which these microbenchmarks are almost meaningless. We are long past counting instructions.