Link to home
Start Free TrialLog in
Avatar of IBTLehman
IBTLehman

asked on

Java compiler "for loop / if statement" optmization

I have the following situation

boolean x=isX();
booleean y=isY();

for(){
  if(x){
     doA();
     if(y)
      doC();    
  }
  else{
    doB();
    if(y)
      doC();    
  }  
}

As you can see the result of the if statements is not dependand on the for loop and are known prior to the loop. I need to make sure the code runs as fast as possible. I can have my if statements outside of the loop and have several different for loops like this

if(x)
   if(y){
      for(){
       doA(); doC();
     }
   }else{
     for(){
       doA();
     }
  }
else
   if(y){
      for(){
       doB(); doC();
     }
   }else{
     for(){
       doB();
     }
   }    

This is messy and can get a lot more messier if I need to add another level of decision making.

My question basically is if java compiler will optimize the first code snippet not do the the logic branching for each loop iteration which will make the first code snippet to be as efficient as the second code snippet. I know that I can run some tests to check on this but I would like to find out for sure and that it is documented somewhere. I purposely ommit the information about our jdk version because I would like to find out the generic answer or the one that shows the differences between different compilers.

thanks



     
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

>>this but I would like to find out for sure and that it is documented somewhere.

I'm not sure if it is documented.
Well, the second approach looks better to me. It is better designed because if the value of 'x' and 'y' don't change, then you should not check them multiple times inside the loop. However, if you put them inside the loop, Java will always check them instead of caching the result, because it is likely that the variables were modified by another method in another thread (if they are data-members of the class), etc.
Avatar of IBTLehman
IBTLehman

ASKER

These variables are method variables, so the thread issues does not apply in our case. The reason I want to use the first form is because the for loop is much bigger then in the pseudo code above and the level of decision making can increase with time.  I believe that having a single loop with if statements inside will make the code cleaner and more understandlable in the long run.
It is possible that the answer here that I cannot do what I want without paying the perfromance price but it would just make sens that today's super-smart compilers will take care of such optimization. And that is what I am trying to figure out.

thanks

ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
this could be easily optimized:
using boolian algebra this would be


doC ()=xy  + x'y = y(x+x') = y(1)=y
doB ()=x'
doA ()=x
so:
for(){
   if(y) doC()
   if(x) doA();
      else doB();
}
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
btw i think checking on true or false is extremly quick compared to initing a for() loop
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>> btw i think checking on true or false is extremly quick compared to initing a for() loop

Yup.
thank you for all your replies. The pseudo code I provided was meant to clearly illustrate the question and not to be taken literally. The actual code is a lot more involved and with its own dependacies. The decision making can go up to 3  - 4 levels which makes the number of possible for loop variations to double digits. The reason the performance is of paramaount importance here is because this code is the back-end for AJAX lookup that I am building. While I do appreciate every response, what I am really looking for is the answer to my question as oppose to offerings of alternative solutions.

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
is this one done?