Assume the following: You got two functions, both doing basically the same thing, which is comparing two random booleans with AND and OR operators. But the one function does this with the normal conditional operators && and ||, the other one with the bitwise operators & and |.
I thought that those two functions would of course take the same time to complete, but they dont. The one with bitwise comparison takes one fifth more time than the one with the "normal" conditional operator.
I was confused and did some research and found the following definition for the conditional operators on the Java documentation from Oracle:
The && and || operators perform Conditional-AND and Conditional-OR operations on two boolean expressions. These operators exhibit "short-circuiting" behavior, which means that the second operand is evaluated only if needed.
&& Conditional-AND
|| Conditional-OR
and for the bitwise operator:
The Java programming language also provides operators that perform bitwise and bit shift operations on integral types.
The bitwise & operator performs a bitwise AND operation.
The bitwise | operator performs a bitwise inclusive OR operation.
Well, those definitions didn't really satisfy me and my question was still not answered. So I did some more research and found a question here on stackoverflow about the effect of bitwise operators on booleans in Java (Effect of a Bitwise Operator on a Boolean in Java), but that didnt really answer my question either.
Here is the code I tested it with, it uses a random number generator to get random booleans to compare with one another and loops over that a loooot of times.
import java.util.Random;
public class Main {
private static final long LOOPS = 100000000L;
private static Random rng = new Random();
public static final void main(String[] args)
{
System.out.println("Bitwise operator: "+loopWithBitwiseOperator(LOOPS));
System.out.println("Normal operator: "+loopWithNormalOperator(LOOPS));
}
public static long loopWithNormalOperator(long loops)
{
long startTime = System.currentTimeMillis();
for (long i = 0L; i < loops; i++)
{
if (rng.nextBoolean() || rng.nextBoolean())
{
i++;
}
if (rng.nextBoolean() && rng.nextBoolean())
{
i++;
}
}
return System.currentTimeMillis()-startTime;
}
public static long loopWithBitwiseOperator(long loops)
{
long startTime = System.currentTimeMillis();
for (long i = 0L; i < loops; i++)
{
if (rng.nextBoolean() | rng.nextBoolean())
{
i++;
}
if (rng.nextBoolean() & rng.nextBoolean())
{
i++;
}
}
return System.currentTimeMillis()-startTime;
}
}
I received the following output:
Bitwise operator: 2502
Normal operator: 1806
So the bitwise operator is in fact slower than the "normal" one. My first idea was that maybe the generated boolean by the random number generator is somehow treated different at runtime and when compared with another boolean it actually bitwisely compares more than needed, as it is undefined how it is actually saved. But then I thought that it might happen because of the "short-circuiting" mechanism for conditional operators, which maybe doesnt apply to bitwise operators which probably actually need both values for comparison.
Is my assumption correct? Why does this happen? Another example-specific behavior that doesnt represent the actual behavior of Java's logical operators?
As always, thanks for any help and clarification in advance.
EDIT:
As requested in the comments, I tried to switch the calls around and include some kind of a warm up phase, but still not much of a change:
Normal operator: 1801
Bitwise operator: 2433