java - What is faster: equal check or sign check -


i wonder operation works faster:

int c = version1.compareto(version2); 

this one

if (c == 1) 

or this

if (c > 0) 

does sign comparasion use 1 bit check , equality comparasion use substraction, or not true? certainty, let's work on x86.

p.s. not optimization issue, wondering how works.

assuming operations jitted x86 opcodes without optimization, there no difference. possible x86 pseudo-assembly snippet 2 cases be:

cmp i, 1 je destination 

and:

cmp i, 0 jg destination 

the cmp operation performs subtraction between 2 operands (register i , immediate 0), discards result , sets flags: positive, negative, overflow etc.

these flags used trigger conditional jump (i.e. jump if condition), in 1 case if 2 operands equal, in second case if first greater second.

again, without considering software (jvm-wise) and/or hardware optimization. in fact, x86_64 architectures have complex pipeline advanced branch-prediction , out-of-order execution, these microbenchmarks meaningless. long past counting instructions.


Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -