Shubham:
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
では、なぜこれが起こるのでしょうか?ループはスキップされているためか?
Nicktar:
コンパイラは、コードを最適化し、変数mを除去することができる死ん店が実際にあることを実現しています。そして、それは場合は、すべてで何かを(メートルを除去した後)と、あまりにもそれを削除しないことを実現しています。同じことがineerループして、外側のループのために行きます。