(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.有时候结果往往出乎多数人预料。