for循环的性能问题

07-04-03 diandidemeng
大家讨论一下,下面两个的循环那个性能好,是为什么,但JVM运行的结果是第二个性能好,为什么啊?

int k = 0;

long start1 = System.currentTimeMillis();

for (int i = 0; i < 1000; i++) {

for (int j = 0; j < 100000; j++) {

k++;

}

}

long end1 = System.currentTimeMillis();

System.out.println(start1);

System.out.println(end1);

System.out.println("K = " + k);

System.out.println("小循环嵌大循环:" + (end1 - start1));

k = 0;

long start2 = System.currentTimeMillis();

for (int i = 0; i < 100000; i++) {

for (int j = 0; j < 1000; j++) {

k++;

}

}

long end2 = System.currentTimeMillis();

System.out.println(start2);

System.out.println(end2);

System.out.println("K = " + k);

System.out.println("大循环嵌小循环:" + (end2 - start2));

zuly
2007-04-03 11:04
这个问题很无聊,oo要从大局看东西,代码效率是C!

猜你喜欢