在下面的代码片段中,
我本来期望
一些细节:
Foo1
是一个类,每次调用方法bar()
时都会增加计数器。Foo2
也是这样做的,但多了一层间接性。我本来期望
Foo1
比Foo2
快,但实际上,Foo2
始终比Foo1
快40%。JVM如何优化代码,使得Foo2
运行比Foo1
更快呢?一些细节:
- 测试是使用
java -server CompositionTest
执行的。 - 使用
java -client CompositionTest
运行测试会产生预期结果,即Foo2
比Foo1
慢。 - 改变循环的顺序没有任何影响。
- 在sun和openjdk的JVM上验证了结果。
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);
}
}