If you study Java a little, you will come up with something called "short-circuit evaluation". this is&&Or||In a logical operator such as, when the left side is evaluated, the boolean value of the entire expression is determined and if it is not necessary to evaluate the right side, the right side is not evaluated. See the following example.
boolean flag1 = false;
boolean flag2 = false;
if(flag1 && flag2){
System.out.println("Both variables are true.");
}
In this program, flag1 and flag2 must both be true in order for the conditional branch on the third line to execute inside the curly braces block. This time flag1 is false, so you can see that you don't have to go inside the curly braces block without having to check the boolean value of flag2. In such a situation, the short-circuit evaluation is a function that automatically prevents the evaluation of flag2.
Java has two similar operators, && and &. The two operators have almost the same functionality, but with some differences. As mentioned earlier, the operator && works as a short-circuit evaluation function. However, short-circuit evaluation does not work for &. That is, & evaluates the value on the right-hand side even in situations where it is not necessary to evaluate the value on the right-hand side.
Java reference books and websites say that && is faster because it doesn't do extra evaluation. When I read that description, I was wondering. "Since it is a different process, is it really faster just to pass the process on the right side?" So I actually tried to verify the difference in processing speed between && and &.
Test1
long start = System.currentTimeMillis();
boolean flag1 = false;
boolean flag2 = false;
for(long i=0; i<10000000000l; i++) {
if(flag1 && flag2) {
System.out.print("");
}
}
long end = System.currentTimeMillis();
System.out.println("Execution time" + (end - start) + "ms");
&& is used in the conditional expression of the if statement.
Test2
long start = System.currentTimeMillis();
boolean flag1 = false;
boolean flag2 = false;
for(long i=0; i<10000000000l; i++) {
if(flag1 & flag2) {
System.out.println("");
}
}
long end = System.currentTimeMillis();
System.out.println("Execution time" + (end - start) + "ms");
& Is used in the conditional expression for if.
I actually ran the above two types of programs and took statistics on the execution time of each program. The table below shows the results.
With short-circuit evaluation(&&) | No short-circuit evaluation(&) | |
---|---|---|
1st time | 3546 | 3500 |
Second time | 3556 | 3595 |
3rd time | 3586 | 3565 |
4th time | 3574 | 3564 |
5th time | 3552 | 3636 |
6th time | 3570 | 3620 |
7th time | 3512 | 3568 |
8th time | 3563 | 3597 |
9th time | 3560 | 3565 |
10th time | 3555 | 3500 |
average | 3557.6 | 3571.0 |
As far as the average of the table is seen, it is considered that the processing speed is faster when the short-circuit evaluation is performed. However, in this experiment, the number of samples was small, and the difference in execution time between with and without short-circuit evaluation was small, so processing is faster using && with short-circuit evaluation than & without short-circuit evaluation. It is a little weak to conclude that it will be.
In the experiment I conducted this time, I got the result that the processing is faster when using the short-circuit evaluation. For the time being, it is better to use && when taking the logical product of multiple boolean values in Java. Also, in other languages such as C, & may have a completely different function of taking both sides as binary numbers and ANDing each digit, so mistakes occur when switching languages. It is recommended to use && if you can get the same result by using & and && in Java to prevent it from increasing.
Recommended Posts