2012-11-16 53 views
0

我在一篇文章中閱讀了這篇文章。但在這裏給出的答案是不明確的..Java緩存和空循環檢測

1. is it true? 
    2. Can anyone explain it better? 
    3. Is there a document that explains java/JVM caching mechanism overall? 


    **Which one is faster in Java ?** 

    for(int i = 100000; i > 0; i--) {} 
    for(int i = 1; i < 100001; i++) {} 


    Answer: Which ever is run second with be fastest. The server JVM can detect and 
    eliminate loops which don't do anything. A method with either loop is compiled when 
    the loop iterates about 10,000 times. (Based on -XX:CompileThreshold=10000) The first 
    loop will take time to detect it doesn't do anything, however the second will have been 
    compiled. 
+1

這個討論可能會幫助你http://stackoverflow.com/questions/7854808/hotspot-jit-optimizations – kosa

+0

它被稱爲優化而不是緩存(這從JVM到JVM有所不同)。此鏈接可以讓你開始http://docs.oracle.com/cd/E15289_01/doc.40/e15058/underst_jit.htm – kosa

+0

這也將幫助你,http://stackoverflow.com/questions/7271147/java-多少時間做一個空循環使用 – Jimmy

回答

0

Java是一種高級語言,所以會出現你寫的代碼,以及由編譯器生成的代碼之間的尊重。編譯器和JVM正試圖優化你的代碼。第一個for不會執行,因爲它不會執行某些操作,並且它不會重複執行超過10000次。第二個用於迭代,但JVM執行它,因爲它迭代了10000次以上。

+0

不準確和不相關 – Bohemian