1

I want to know whether there is a performance difference in following two code blocks

1>

 if(name == null) {
    //something
    }

    if(name != null) {
    //something
    }

and

2>

 if(name == null) {
    //something
    }

    else {
    //something
    }
1
  • Does Findbugs issue a warning about this?
    – Thilo
    Mar 2, 2011 at 9:49

9 Answers 9

5

The first compares twice, the second compares once. The difference will not be noticeable, but it's there.

5
  • 1
    +1 for "The difference will not be noticeable, but it's there."
    – jensgram
    Mar 2, 2011 at 9:16
  • Well, that depends on the code of course :D - if it's in a tight loop then you could double the time complexity!
    – Nick
    Mar 2, 2011 at 9:17
  • 2
    @Nick: Yes, if all you did was comparing. I think the OP's //something may be more relevant for performance than the if statements.
    – Erik
    Mar 2, 2011 at 9:19
  • 2
    Also, unless name is final, the first //something could set it to not null, in which case the two code fragments are not equivalent (it could enter both blocks)
    – Thilo
    Mar 2, 2011 at 9:44
  • @Thilo: if name is a field (particularly a volatile one), then you could also have the problem of other threads manipulating name while //something is running. Mar 2, 2011 at 9:49
5

after benchmarkint it on 100.000.000 iterations, the first execution costs 719ms and the second 703ms.

I used a modulo so the conditions has to change every turn and avoid precompiled result. Please find the code below. I have noticed that this gap reduces when number of iterations increases.

public static void main(String[] args) {
  Date start1 = new Date();

  for(int i=0; i<100000000; i++) {
    int it = i%2;
    if(it == 0) {
      double j = Math.random();
      j++;
    }
    if(it != 0) {
      double j = Math.random();
      j++;
    }   
  }

  Date end1 = new Date();
  Date start2 = new Date();    

  for(int i=0; i<10000000; i++) {
    int it = i%2;
    if(it == 0) {
      double j = Math.random();
      j++;
    } else {
      double j = Math.random();
      j++;
    } 
  }

  Date end2 = new Date();

  System.out.println((end1.getTime()-start1.getTime())+" / "+(end2.getTime()-start2.getTime()));
}
5
  • +1 - for benchmarking. Could you please post your benchmark so that others can validate it / run it.
    – Stephen C
    Mar 2, 2011 at 9:25
  • but there is a calculation. the question would be whether the precompilation is able to identify the equality of the two examples of the question.
    – bertolami
    Mar 2, 2011 at 9:50
  • @bertolami I am not really competent in precompilation tips, but I know that java can understand and ignore "unreachable code". That's why i added calculation instead of null test.
    – vaugham
    Mar 2, 2011 at 9:57
  • but at least you should substract the time it takes for 10000000 times calculating i%2
    – bertolami
    Mar 2, 2011 at 11:46
  • Right, I have amended this. There is still some difference due to the supplementary comparison.
    – vaugham
    Mar 2, 2011 at 12:24
2

Just a brief comment to say that the compiler cannot optimize it in all cases, because name is visible within the first if block therefore it could have been modified in it, so it has to be checked again in the second if condition. Imagine this case:

if (name == null) {
    // Does something
    name = "Did it.";
}
if (name != null) {
   // Does something else
}

It's clearly not equivalent to

if (name == null) {
    // Does something
    name = "Did it.";
} else {
   // Does something else
}

If what you actually mean is that you should do something in one case and something else otherwise, please use if { ... } else { ... } - not just for (minimal) performance improvement, but also because your code should reflect what you actually mean.

1
  • +1. Use the construct that is appropriate here (and make as many variables as possible final) in order to convey the intent clearly and avoid subtle bugs.
    – Thilo
    Mar 2, 2011 at 9:51
1

Note that the two fragments are not necessarily equivalent, because the first block could re-assign name so that the second condition will also be true.

This can introduce hard to spot bugs, so I suggest that (before thinking about performance), you think about making the variable final if possible and use if/else when it makes sense (i.e. it should enter only one of the two branches) and chained if's when that makes sense (for example when the first if can establish a default value for the next one to use).

0

Yes there will, on the second one only one condition will be checked and on the first one two conditions would have to be checked.

An if clause that fails its evaluation has to make an "instruction jump" even if there is no else statement follwing it.

Assuming the first if is false, you'd be comparing these 2 execution scenarios:

1>
Check 1st condition
Skip to check 2nd condition
Do "something" inside the 2nd condition

2>
Check condition
Skip to "something" inside the else
1
  • I think you mean the other way around!
    – Nick
    Mar 2, 2011 at 9:16
0

Yes becuase both if cases will be evaluated in the first whereas only one if will be evaluated in the second.

0

yes, there will be a difference: in the second example, tehre's only 1 statement to be proofed, in the first one there are two.

but: the difference in performance will be absolutely minimal, in 99% of the cases you won't even notive any difference - make sure your code is as readable as it can be, thats much more important ;)

0

yes obviously the second code will perform inconsiderably better, because there is only one condition to check

6
  • 3
    @selvin - it is not obvious. The compiler could deduce that the second test is redundant and optimize it out.
    – Stephen C
    Mar 2, 2011 at 9:24
  • @Stephen: however, the two code blocks are not equivalent. Especially if name is a volatile field. Mar 2, 2011 at 9:47
  • @Stephen most of the compilers aren't smart enough to optimize it automatically. @Joachim what would happen if name is volatile
    – Selvin
    Mar 2, 2011 at 9:53
  • @selvin - my point is that the answer is not "obvious" ... as you claimed.
    – Stephen C
    Mar 2, 2011 at 10:00
  • 1
    BTW - volatile is only one case where the optimization might be invalid. Others are where name is updated by the first "then" block, or where some other thread updates it and the correct steps are taken to synchronize ... per the memory model.
    – Stephen C
    Mar 2, 2011 at 10:07
-1

I believe the compiler is smart enough to notice that the second if in the first example is redundant, so there won't be any performance change

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.