preface

Today I recall a Java interview question:

What’s the most efficient way to figure out 2 times 8?

The answer is 2 << 3, using bits, which is the same thing as multiplying by 2^3.

I was discussing this with my friends, and someone said that the Java compiler would optimize the operations involving powers of 2, such as /2, /4, /8, into bitwise operations. Online inquiry found that there is no number of relevant articles, holding the spirit of inquiry, decided to manual test.

test

code

public class Main {
    public static void main(String[] args) {
        Main.testDivision();
        Main.testShift();
    }

    /** * division test */
    private static void testDivision(a) {
        long startTime = System.nanoTime();
        int tem = Integer.MAX_VALUE;
        for (int i = 0; i <1000000000 ; i++) {
            tem = tem / 2;
        }
        long endTime = System.nanoTime();
        System.out.println(String.format("Division operations consumed time: %f s", (endTime - startTime) / 1000000000.0));
    }

    /** * bit operation test */
    private static void testShift(a) {
        long startTime = System.nanoTime();
        int tem = Integer.MAX_VALUE;
        for (int i = 0; i <1000000000 ; i++) {
            tem = tem >> 1;
        }
        long endTime = System.nanoTime();
        System.out.println(String.format("Shift operations consumed time: %f s", (endTime - startTime) / 1000000000.0)); }}Copy the code

The test results

A total of 3 tests were performed:

// First Division Operations Consumed Time: 1.023045s Shift Operations Consumed Time: 0.264115 S // secondary Division Operations Consumed Time: 1.000502 s Shift Operations Consumed Time: 0.251574s // Third Division Operations Consumed Time: 1.056946s Shift Operations Consumed Time: 0.262253 sCopy the code

Based on the elapsed time, it is concluded that the Java compiler does not optimize /2 for bitwise operations.

Other tests

Tests whether the Java compiler optimizes modulo/mod of 2 to bitwise

In Java, the modulo/mod operator is %, usually also called mod operations, they all follow the division rule, return the remainder of the left operand divided by the right operand.

code

public class Main {
    public static void main(String[] args) {
        Main.testModulo();
        Main.testShift();
    }

    /** * test */
    private static void testModulo(a) {
        long startTime = System.nanoTime();
        int tem = Integer.MAX_VALUE;
        for (int i = 0; i <1000000000 ; i++) {
            tem = tem % 2;
        }
        long endTime = System.nanoTime();
        System.out.println(String.format("Modulo operations consumed time: %f s", (endTime - startTime) / 1000000000.0));
    }

    /** * bit operation test */
    private static void testShift(a) {
        long startTime = System.nanoTime();
        int tem = Integer.MAX_VALUE;
        for (int i = 0; i <1000000000 ; i++) {
            tem = tem & 1;
        }
        long endTime = System.nanoTime();
        System.out.println(String.format("Shift operations consumed time: %f s", (endTime - startTime) / 1000000000.0)); }}Copy the code

The test results

A total of 3 tests were performed:

Modulo Operations Consumed Time: 0.813894 s Shift Operations Consumed Time: 0.003005 S // secondary Modulo Operations Consumed Time: 0.808596 s Shift Operations Consumed Time: 0.002247s // Third Modulo Operations Consumed Time: 0.825826s Shift Operations Consumed Time: 0.003162sCopy the code

Based on the elapsed time, it is concluded that the Java compiler has not optimized the modulo/mod operation of 2 into a bit operation.

Write in the last

Thank you for reading. If you have any questions about the content of this article, please feel free to contact me! — [email protected]