(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.有時候結果往往出乎多數人預料。