3

To compare how short circuit evaluation logical operators, for example &&, perform compared to bitwise evaluation logical operators, i.e. &, I wrote this example and ran it:

package examples1;

import java.util.Scanner;

public class ShortCircuitOperatorsVSBitwiseOperators {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter A: "); boolean a = scanner.nextBoolean();
        System.out.print("Enter B: "); boolean b = scanner.nextBoolean();
        long startTimeShortCircuited = System.currentTimeMillis();
        boolean resultShortCircuited = true;
        for(long i = 0; i < 10000000000L; i++) {
            resultShortCircuited = a && b;
        }
        long endTimeShortCircuited = System.currentTimeMillis();
        System.out.println(resultShortCircuited + " in " + (endTimeShortCircuited - startTimeShortCircuited) + " milliseconds, short-circuited");

        long startTimeBitwise = System.currentTimeMillis();
        boolean resultBitwise = true;
        for(long i = 0; i < 10000000000L; i++) {
            resultBitwise = a & b;
        }
        long endTimeBitwise = System.currentTimeMillis();
        System.out.println(resultBitwise + " in " + (endTimeBitwise - startTimeBitwise) + " milliseconds, bitwise");
        scanner.close();
    }
}

An example run shows the following:

java examples1/ShortCircuitOperatorsVSBitwiseOperators
Enter A: false
Enter B: true
false in 4829 milliseconds, short-circuited
false in 3276 milliseconds, bitwise

This doesn't make sense. I would expect the short circuit evaluation to be faster since it doesn't evaluate the right side of the && in this case if the left side was false. What is the reason for the counter-intuitive results?

Shant Dashjian
  • 888
  • 11
  • 20
  • Good question, but both performance numbers seem similar. Have you checked to see if it's reproducible? Also, what happens with a smaller/larger loop? – Tim Biegeleisen Apr 15 '18 at 05:42
  • Yes, it is reproducible. With a smaller loop, short circuit evaluation is still slower, as is the case with a larger loop. – Shant Dashjian Apr 15 '18 at 06:05
  • 1
    Congratulations, you have created an incorrect microbenchmark. Just change the order of the two operations (first do bitwise, then do short-circuited) and you get the opposite result. This is because of Java startup time, HotSpot compilation, and many other things. – Erwin Bolwidt Apr 15 '18 at 06:18
  • 1
    Microbenchmarking is hard. I suggest that you look at JMH as a framework for correct microbenchmarks. https://stackoverflow.com/questions/504103/how-do-i-write-a-correct-micro-benchmark-in-java – Erwin Bolwidt Apr 15 '18 at 06:18
  • 1
    @ErwinBolwidt I did change the order per your comment and short-circuited evaluation is still slower than bitwise. This is the first time I hear about the concept of microbenchmarking so that is worth looking at regardless. Thanks. – Shant Dashjian Apr 15 '18 at 06:50

1 Answers1

1

Short circuited operation is complicated.

public static boolean shortCircuitedAnd(boolean a, boolean b) {
    return a && b;
}

public static boolean bitwiseAnd(boolean a, boolean b) {
    return a & b;
}

They are compiled to

public static boolean shortCircuitedAnd(boolean, boolean);
  Code:
     0: iload_0
     1: ifeq          10
     4: iload_1
     5: ifeq          10
     8: iconst_1
     9: ireturn
    10: iconst_0
    11: ireturn

public static boolean bitwiseAnd(boolean, boolean);
  Code:
     0: iload_0
     1: iload_1
     2: iand
     3: ireturn
  • Then why does the short-circuit evaluation take less time if you do it after the bitwise evaluation (the reverse of the order that the OP is doing the test in) ? – Erwin Bolwidt Apr 15 '18 at 06:35
  • @ErwinBolwidt I don't know why. It is not OP's question. If you want to know why, you should post as your own question. –  Apr 15 '18 at 07:23
  • I was asking you this question so you could realize this what you posted does not answer the OP's question. Number of bytecode instructions has very little relation with how fast something executes; if you swap the order of the tests, then the one with more bytecode instructions executes faster. – Erwin Bolwidt Apr 15 '18 at 07:50
  • If so please do not ask me. SO is not a site to test people. –  Apr 15 '18 at 07:58