Java中利用final關鍵字inline編譯最佳化真的有效嗎?

來源:互聯網
上載者:User

(inkfish原創,轉載請註明出處:http://blog.csdn.net/inkfish/)

  為尋求java代碼的效能最佳化,從網上搜到利用final關鍵字進行編譯時間inline最佳化的方法,但是真的有效嗎。實際測試中發現未必,甚至效能影響巨大,最終放棄了使用final最佳化的想法。

  測試環境:Windows XP SP2,JDK 1.6.0_15-b03,Eclipse 3.5 SR1。

 

package test; public class Test { public static void main(String[] args) { int loop = 10000000;//測試1000萬迴圈 String num = "0"; //----第一遍---- { System.out.print("第一遍:/t"); long start = System.currentTimeMillis(); for (int i = 0; i < loop; i++) { Class1 c1 = new Class1(); c1.setF1(num); c1.getF1(); } System.out.printf("%5dms", (System.currentTimeMillis() - start)); start = System.currentTimeMillis(); for (int i = 0; i < loop; i++) { Class2 c2 = new Class2(); c2.setF1(num); c2.getF1(); } System.out.printf("%5dms", (System.currentTimeMillis() - start)); start = System.currentTimeMillis(); Class1 c1 = new Class1(); for (int i = 0; i < loop; i++) { c1.setF1(num); c1.getF1(); } System.out.printf("%5dms", (System.currentTimeMillis() - start)); start = System.currentTimeMillis(); Class2 c2 = new Class2(); for (int i = 0; i < loop; i++) { c2.setF1(num); c2.getF1(); } System.out.printf("%5dms", (System.currentTimeMillis() - start)); } //----第二遍---- { System.out.print("/n第二遍:/t"); long start = System.currentTimeMillis(); for (int i = 0; i < loop; i++) { Class1 c1 = new Class1(); c1.setF1(num); c1.getF1(); } System.out.printf("%5dms", (System.currentTimeMillis() - start)); start = System.currentTimeMillis(); for (int i = 0; i < loop; i++) { Class2 c2 = new Class2(); c2.setF1(num); c2.getF1(); } System.out.printf("%5dms", (System.currentTimeMillis() - start)); start = System.currentTimeMillis(); Class1 c1 = new Class1(); for (int i = 0; i < loop; i++) { c1.setF1(num); c1.getF1(); } System.out.printf("%5dms", (System.currentTimeMillis() - start)); start = System.currentTimeMillis(); Class2 c2 = new Class2(); for (int i = 0; i < loop; i++) { c2.setF1(num); c2.getF1(); } System.out.printf("%5dms", (System.currentTimeMillis() - start)); } //----第三遍---- { System.out.print("/n第三遍:/t"); long start = System.currentTimeMillis(); for (int i = 0; i < loop; i++) { Class1 c1 = new Class1(); c1.setF1(num); c1.getF1(); } System.out.printf("%5dms", (System.currentTimeMillis() - start)); start = System.currentTimeMillis(); for (int i = 0; i < loop; i++) { Class2 c2 = new Class2(); c2.setF1(num); c2.getF1(); } System.out.printf("%5dms", (System.currentTimeMillis() - start)); start = System.currentTimeMillis(); Class1 c1 = new Class1(); for (int i = 0; i < loop; i++) { c1.setF1(num); c1.getF1(); } System.out.printf("%5dms", (System.currentTimeMillis() - start)); start = System.currentTimeMillis(); Class2 c2 = new Class2(); for (int i = 0; i < loop; i++) { c2.setF1(num); c2.getF1(); } System.out.printf("%5dms", (System.currentTimeMillis() - start)); } } } class Class1 { private String f1; public String getF1() { return f1; } public void setF1(String f1) { this.f1 = f1; } } final class Class2 { private String f1; public final String getF1() { return f1; } public final void setF1(String f1) { this.f1 = f1; } }

運行結果:

第一遍: 110ms 1203ms 16ms 156ms 第二遍: 94ms 1203ms 15ms 141ms 第三遍: 94ms 1203ms 15ms 141ms

 

總結:

1.利用final進行inline最佳化無效;

2.有時候結果往往出乎多數人預料。

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.