开发者社区> 问答> 正文

为什么跳过for循环?

long start = System.currentTimeMillis();
long m = 0;
System.out.println("Going into the loop");

for (int i = 0; i < 10000000; i++) {
    for (int j = 0; j < 1000; j++) {
        if (i % 2 == 0 && j % 3 == 0) {
            m = i + j;
        }
    }
}

System.out.println("Out of the loop");

long end = System.currentTimeMillis();
System.out.println("Time : " + (end - start));
System.out.println("m : " + m);

当我运行上述代码时,“ m”值评估为10000997,运行时间为13321。

Going into the loop
Out of the loop
Time : 13321
m : 10000997

但是,当我评论最后一个打印“ m”值的SOP语句时,运行时间大约为7。

Going into the loop
Out of the loop
Time : 7

那么为什么会这样呢?是否跳过了for循环?

展开
收起
垚tutu 2019-12-19 16:35:18 664 0
1 条回答
写回答
取消 提交回答
  • 精于基础,广于工具,熟于业务。

    看一下你的M值的,再循环体外增加一个值的输出对你的循环不会产生影响的。想看一个循环是否有中间跳出,再循环里面输出一下M的值吧。

    2019-12-19 16:55:29
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载