非常奇怪的結果!
。。!
測試方法如以下
public class Main {
public static void main(String[] args){
long ti = System.currentTimeMillis();
for(int i=Integer.MIN_VALUE;i<Integer.MAX_VALUE;i++){
if(i==i) continue;
}
System.out.println(System.currentTimeMillis()-ti);
ti = System.currentTimeMillis();
for(int i=Integer.MIN_VALUE;i<Integer.MAX_VALUE;i++){
continue;
}
System.out.println(System.currentTimeMillis()-ti);
}
}
輸出結果
6 1320
問題1:
在for循環中增加推斷后,循環運行的時間居然差這么多。。
以上是問題,看以下這個測試,你會發現更有意思
public class Main {
public static void main(String[] args){
long ti = System.currentTimeMillis();
long c = 0;
for(int i=Integer.MIN_VALUE;i<Integer.MAX_VALUE;i++){
c++;
if(i==i) continue;
}
System.out.println(System.currentTimeMillis()-ti + " "+c);
ti = System.currentTimeMillis();
c=0;
for(int i=Integer.MIN_VALUE;i<Integer.MAX_VALUE;i++){
c++;
continue;
}
System.out.println(System.currentTimeMillis()-ti+ " "+c);
}
}
相同輸出
93 4294967295 7847 4294967295 不過在循環內做了一次++操作,耗時居然添加了近10倍! 不得不說,java!你的執行效率實在是慘不忍睹。。。
。
java版本號: javac 1.7.0_25
求解釋:
在for循環中增加推斷與不加推斷,運行時間相差為什么會相差200倍?!
-------------------------------------------------------------------------------------------------
今天又測試一次,java版本號不知道什么時候變成了這個樣子: javac 1.7.0_55 結果不再和上次測試一樣,看來上次是遇到bug了。
。
不知道java當自己升級。。
版權聲明:本文博主原創文章。博客,未經同意,不得轉載。
