while和for的效率問題

今天突發奇想試了一下這麼一個代碼:
public class Test {
public static void main(String[] args) {
long count  = 10000000000L;//1000000000L;//100000000L;//10000000L;
long i = 0;
Date d1 = new Date();
while(i<count){
i++;
}
i=0;
Date d2 = new Date();
for(;i<count;){
i++;
}
Date d3 = new Date();
System.out.println(d2.getTime()-d1.getTime());
System.out.println(d3.getTime()-d2.getTime());
}
}

count的四個值的測試都發現結果for比while快,很奇怪,有誰可以告訴我原因麼?
while和for的效率問題 - coffee_hc - coffee_hc的博客
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章