Ruchira Gayan Ranaweera
Ruchira Gayan Ranaweera

Reputation: 35557

Different between two cases here

I am just thinking, is there any performance wise benefit between following scenarios.

Case 1

int x = 2;
boolean y = false;

if (x == 2) {
  y = true;
}

Case 2

int x = 2;
boolean y = (x == 2);

My idea is, case 1 is more readable.

Upvotes: 2

Views: 82

Answers (3)

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 279940

Peter started, but didn't finish. For reference (Java 7 compiler), this

public class Test {
    public static void main(String[] args) {
        int x = 2;
        boolean y = (x == 2);
    }
}

compiles to

  public static void main(java.lang.String[]);
    Code:
       0: iconst_2           // load the int value 2 onto the stack
       1: istore_1           // store int value into variable 1
       2: iload_1            // load an int value from local variable 1
       3: iconst_2           // load the int value 2 onto the stack
       4: if_icmpne     11   // if ints are not equal, branch to instruction at branchoffset 
       7: iconst_1           // load the int value 1 onto the stack (true)
       8: goto          12
      11: iconst_0           // load the int value 0 onto the stack (false)
      12: istore_2           // store int value into variable 2
      13: return 

while

public class Test {
    public static void main(String[] args) {
        int x = 2;
        boolean y = false;

        if (x == 2) {
          y = true;
        }
    }

}

compiles to

 public static void main(java.lang.String[]);
    Code:
       0: iconst_2          // load the int value 2 onto the stack
       1: istore_1          // store int value into variable 1
       2: iconst_0          // load the int value 0 onto the stack (false)
       3: istore_2          // store int value into variable 2
       4: iload_1           // load an int value from local variable 1
       5: iconst_2          // load the int value 2 onto the stack
       6: if_icmpne     11  // if ints are not equal, branch to instruction at branchoffset
       9: iconst_1          // load the int value 1 onto the stack
      10: istore_2          // store int value into variable 2
      11: return    

Both have a branching statement.

To answer the question, you really won't gain anything.

Upvotes: 3

in both cases you are

  • comparing x to a literal 2
  • setting y to a value

but only in the first case do you have a branch. In the second case, you merely set y to the result of the comparison.

Case 2 does less operations because of this.

But the compiler probably optimizes it that way for you anyway.

Upvotes: 4

Óscar López
Óscar López

Reputation: 236004

There will be a tiny, tiny difference in performance (the first version adds an additional if instruction after all, but even that might get optimized away by the static compiler or the JIT compiler), but anyway it'll be negligible. For such a simple case, it's better to optimize for readability, forget about micro optimizations.

Upvotes: 6

Related Questions