黑马程序员技术交流社区
标题:
Java 执行效率测试
[打印本页]
作者:
王松朝
时间:
2014-7-31 18:26
标题:
Java 执行效率测试
本帖最后由 王松朝 于 2014-7-31 18:28 编辑
很奇怪的结果!!!!
测试程序如下
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
复制代码
在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倍?!
注:转自我的blog
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2