在下面的代码片段中,Foo1
是一个在每次bar()
调用该方法时递增一个计数器的类。Foo2
做同样的事情,但多了一层间接性。
我希望Foo1
比 快Foo2
,但在实践中,Foo2
始终比 快 40% Foo1
。JVM 是如何优化代码以使其Foo2
运行速度快于Foo1
?
一些细节
- 测试是用
java -server CompositionTest
. - 运行测试
java -client CompositionTest
会产生预期的结果,Foo2
比Foo1
. - 切换循环的顺序并没有什么不同。
- 结果在 sun 和 openjdk 的 JVM 上都使用 java6 进行了验证。
编码
public class CompositionTest {
private static interface DoesBar {
public void bar();
public int count();
public void count(int c);
}
private static final class Foo1 implements DoesBar {
private int count = 0;
public final void bar() { ++count; }
public int count() { return count; }
public void count(int c) { count = c; }
}
private static final class Foo2 implements DoesBar {
private DoesBar bar;
public Foo2(DoesBar bar) { this.bar = bar; }
public final void bar() { bar.bar(); }
public int count() { return bar.count(); }
public void count(int c) { bar.count(c); }
}
public static void main(String[] args) {
long time = 0;
DoesBar bar = null;
int reps = 100000000;
for (int loop = 0; loop < 10; loop++) {
bar = new Foo1();
bar.count(0);
int i = reps;
time = System.nanoTime();
while (i-- > 0) bar.bar();
time = System.nanoTime() - time;
if (reps != bar.count())
throw new Error("reps != bar.count()");
}
System.out.println("Foo1 time: " + time);
for (int loop = 0; loop < 10; loop++) {
bar = new Foo2(new Foo1());
bar.count(0);
int i = reps;
time = System.nanoTime();
while (i-- > 0) bar.bar();
time = System.nanoTime() - time;
if (reps != bar.count())
throw new Error("reps != bar.count()");
}
System.out.println("Foo2 time: " + time);
}
}