代碼:
class test
{
public static void main(String abc[])
{
for(int k=1; k<=3; k++)
{
for(int N=1; N<=1_000_000_000; N=N*10)
{
long t1 = System.nanoTime();
int j=1;
for(int i=0; i<=N; i++)
j=j*i;
long t2 = System.nanoTime() - t1;
System.out.println("Time taken for "+ N + " : "+ t2);
}
}
}
}
以上代碼的輸出:
Time taken for 1 : 2160
Time taken for 10 : 1142
Time taken for 100 : 2651
Time taken for 1000 : 19453
Time taken for 10000 : 407754
Time taken for 100000 : 4648124
Time taken for 1000000 : 12859417
Time taken for 10000000 : 13706643
Time taken for 100000000 : 136928177
Time taken for 1000000000 : 1368847843
Time taken for 1 : 264
Time taken for 10 : 233
Time taken for 100 : 332
Time taken for 1000 : 1562
Time taken for 10000 : 17341
Time taken for 100000 : 136869
Time taken for 1000000 : 1366934
Time taken for 10000000 : 13689017
Time taken for 100000000 : 136887869
Time taken for 1000000000 : 1368178175
Time taken for 1 : 231
Time taken for 10 : 242
Time taken for 100 : 328
Time taken for 1000 : 1551
Time taken for 10000 : 13854
Time taken for 100000 : 136850
Time taken for 1000000 : 1366919
Time taken for 10000000 : 13692465
Time taken for 100000000 : 136833634
Time taken for 1000000000 : 1368862705
在循環中,即使價值我從0開始,表示產品爲零,沒有JIT優化。爲什麼不 ? 在上面提供的鏈接中,我以前將for循環放在JIT正在優化的方法調用中。是否將這些陳述放在優化過程中的方法中?
這並不覺得自己是一個很現實的問題。你正在做一些無用的事情 - JIT優化對真正的*代碼代碼真的很重要嗎? –
您正在測量的時間以納秒爲單位,因此速度足夠快。在這裏查看修改後的測試的毫秒數 - http://ideone.com/Xq0xj1 –