I was implementing a snippet on Leetcode and my initial loop ran like this:
//n and x are an integer and a double respectively
long N = Math.abs((long)n);
double result = 1;
while(N != 0){
if((N & 1) == 1){
result *= x;
}
N = N >> 1;
x *= x;
}
The whole code took 2ms to run. I then changed N != 0
to N > 0
and the code took 1ms to run. Why was there a jump in runtime due to this change? In other words, how does Java implement x != y
and x > y
?