奇怪了,奇案了,居然还慢了。

孤尽JavaSea 2014-09-12 10:28:39
public class SecondsCalc {

public static void main(String[] args) {
long a = System.currentTimeMillis();
for(int i=0; i<5000000; i++){
if(1 < (7 * 24 * 60 * 60 * 1000)){ //改成if(1 < 604800000){ 执行时间反而更长了。何解????
// if(1 < 604800000){
System.out.println("ok" + i);
}
}
long b = System.currentTimeMillis();
System.out.println("time=" + (b-a));
}

}
...全文
299 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
咔啪咔啪 2014-09-13
  • 打赏
  • 举报
回复
#8说的没错!! 你可以反编译class文件看看,这里的if判断被果断跳过啊!
rumlee 2014-09-13
  • 打赏
  • 举报
回复
引用 8 楼 kiyoki 的回复:
[quote=引用 5 楼 rumlee 的回复:] 这两行代码的效果是一模一样的 即时这样写 if(1 < (7 * 24 * 60 * 60 * 1000)) 编译器也会优化成 if(1 < 604800000) 至于说你执行时间有差距,是因为你没有办法排除所有的外来干扰。
此言差矣,无论是 1 < (7 * 24 * 60 * 60 * 1000) 还是 if(1 < 604800000) 编译器都会发现结果是true,跳过这段直接编译,实际上编译后执行的就会是: for(int i=0; i<5000000; i++){ System.out.println("ok" + i); } 执行时长和这两个半毛钱关系都没有[/quote] 我以为他写的是i<,我感觉楼主应该也是想写i<吧,写1<也没啥意义啊。
引用 7 楼 Java_gannbare 的回复:
[quote=引用 5 楼 rumlee 的回复:] 这两行代码的效果是一模一样的 即时这样写 if(1 < (7 * 24 * 60 * 60 * 1000)) 编译器也会优化成 if(1 < 604800000) 至于说你执行时间有差距,是因为你没有办法排除所有的外来干扰。
你怎么确定是优化过的,你是猜的吧?有证据吗?[/quote] 我想告诉你的是我当然有证据。 我写的一段代码,源码如下:

	public static void main(String[] args) {
		int i=0;
		if(i<3*7){
			System.out.println("aaa");
		}
		if(1<3*7){
			System.out.println("bbb");
		}
	}
编译之后,再反编译的代码如下:

  public static void main(String[] args)
  {
    int i = 0;
    if (i < 21) {
      System.out.println("aaa");
    }

    System.out.println("bbb");
  }

kiyoki 2014-09-13
  • 打赏
  • 举报
回复
引用 5 楼 rumlee 的回复:
这两行代码的效果是一模一样的 即时这样写 if(1 < (7 * 24 * 60 * 60 * 1000)) 编译器也会优化成 if(1 < 604800000) 至于说你执行时间有差距,是因为你没有办法排除所有的外来干扰。
此言差矣,无论是 1 < (7 * 24 * 60 * 60 * 1000) 还是 if(1 < 604800000) 编译器都会发现结果是true,跳过这段直接编译,实际上编译后执行的就会是: for(int i=0; i<5000000; i++){ System.out.println("ok" + i); } 执行时长和这两个半毛钱关系都没有
孤尽JavaSea 2014-09-13
  • 打赏
  • 举报
回复
引用 5 楼 rumlee 的回复:
这两行代码的效果是一模一样的 即时这样写 if(1 < (7 * 24 * 60 * 60 * 1000)) 编译器也会优化成 if(1 < 604800000) 至于说你执行时间有差距,是因为你没有办法排除所有的外来干扰。
你怎么确定是优化过的,你是猜的吧?有证据吗?
姜小白- 2014-09-12
  • 打赏
  • 举报
回复
楼主试一下System.out.println(System.currentTimeMillis() + " " + new Date().getTime()); 这行代码 Date 的实现中取得时间就是System.currentTimeMillis() 放在同一行,有时两个数值会相同,有时后面会比前面慢一点点 这个差异就是计算机的cpu造成的,指不定哪次执行时,cpu空闲,就快一点,哪次cpu忙,执行的就慢一点 楼主的情况,跟这个是差不多的
rumlee 2014-09-12
  • 打赏
  • 举报
回复
这两行代码的效果是一模一样的 即时这样写 if(1 < (7 * 24 * 60 * 60 * 1000)) 编译器也会优化成 if(1 < 604800000) 至于说你执行时间有差距,是因为你没有办法排除所有的外来干扰。
孤尽JavaSea 2014-09-12
  • 打赏
  • 举报
回复
引用 2 楼 bree06 的回复:
是因为System.out.println("ok" + i);引起的。print调外部设备那是java不可控的。 这样试试
        long a = System.currentTimeMillis();
        long count = 0;
        for(int i=0; i<500000000; i++){
            if(1 < (7 * 24 * 60 * 60 * 1000)) {  //改成if(1 < 604800000){  执行时间反而更长了。何解????
//		            if(1 < 604800000){
//                System.out.println("ok" + i);
            	count += i;
            }
        }
        long b = System.currentTimeMillis();
        System.out.println("count is :" + count + ". time=" + (b-a));
================ 还是差不多的。
姜小白- 2014-09-12
  • 打赏
  • 举报
回复
两次的class文件同为

  public static void main(String[] args)
  {
    long a = System.currentTimeMillis();
    for (int i = 0; i < 5000000; ++i)
    {
      System.out.println("ok" + i);
    }

    long b = System.currentTimeMillis();
    System.out.println("time=" + (b - a));
  }
时间差,是由系统环境引起的,不是代码引起的
bree06 2014-09-12
  • 打赏
  • 举报
回复
是因为System.out.println("ok" + i);引起的。print调外部设备那是java不可控的。 这样试试
        long a = System.currentTimeMillis();
        long count = 0;
        for(int i=0; i<500000000; i++){
            if(1 < (7 * 24 * 60 * 60 * 1000)) {  //改成if(1 < 604800000){  执行时间反而更长了。何解????
//		            if(1 < 604800000){
//                System.out.println("ok" + i);
            	count += i;
            }
        }
        long b = System.currentTimeMillis();
        System.out.println("count is :" + count + ". time=" + (b-a));
The_end90 2014-09-12
  • 打赏
  • 举报
回复
我刚跑了一下 if(1 < (7 * 24 * 60 * 60 * 1000)) //time=60650 if(1 < 604800000) //60263

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧