Output timing problem

the following code:

String str1="asdfavaxzvzxvc";
String str2="werwerzsfaasdf";
Object c=str1;
Object d=str2;
System.out.println(c);
long time1=System.currentTimeMillis();
for(int i=0;i<1000000000;i++){
    if(c.equals(d)){
        //System.out.println("asfasdfasdf"); // line 9
    }
}
long time2=System.currentTimeMillis();
System.out.println("time taken in this is "+(time2-time1));

When I uncomment the line 9, that is let print if condition is true, though never it is going to happen since both object are not equal , then it takes 5000+ milli-seconds, and to my surprise by just commenting it takes only 5 milli-seconds, I am not getting the reason, why it takes so much time if it is not commented, since it's never going to get executed...

Is this some sort of branch prediction effect ? or any sort of compiler optimization

9
задан Trufa 12 May 2011 в 17:46
поделиться