今天突发奇想试了一下这么一个代码:
count的四个值的测试都发现结果for比while快,很奇怪,有谁可以告诉我原因么?
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());
}
}
![while和for的效率问题 - coffee_hc - coffee_hc的博客 while和for的效率问题 - coffee_hc - coffee_hc的博客](http://img.tongji.linezing.com/2438093/tongji.gif)