我怀疑这是某些 Java 版本的功能。
如果我运行以下
public class Main {
public static final int RUNS = 100000000;
static final ThreadLocal<StringBuilder> STRING_BUILDER_THREAD_LOCAL = new ThreadLocal<StringBuilder>() {
@Override
protected StringBuilder initialValue() {
return new StringBuilder();
}
};
public static final StringBuilder myStringBuilder() {
StringBuilder sb = STRING_BUILDER_THREAD_LOCAL.get();
sb.setLength(0);
return sb;
}
public static long testSeparate(String x, String y, String z) {
long start = System.nanoTime();
for (int i = 0; i < RUNS; i++) {
StringBuilder s = myStringBuilder();
s.append(x)
.append(y)
.append(z);
dontOptimiseAway = s.toString();
}
long time = System.nanoTime() - start;
return time;
}
public static long testChained(String x, String y, String z) {
long start = System.nanoTime();
for (int i = 0; i < RUNS; i++) {
StringBuilder s = myStringBuilder();
s.append(x);
s.append(y);
s.append(z);
dontOptimiseAway = s.toString();
}
long time = System.nanoTime() - start;
return time;
}
static String dontOptimiseAway = null;
public static void main(String... args) {
for (int i = 0; i < 10; i++) {
long time1 = testSeparate("x", "y", "z");
long time2 = testChained("x", "y", "z");
System.out.printf("Average time separate %.1f ns, chained %.1f ns%n",
(double) time1 / RUNS, (double) time2 / RUNS);
}
}
}
使用 Java 7 更新 4
Average time separate 49.8 ns, chained 49.0 ns
Average time separate 50.7 ns, chained 49.3 ns
Average time separate 46.9 ns, chained 46.5 ns
Average time separate 46.6 ns, chained 46.4 ns
Average time separate 46.6 ns, chained 46.6 ns
Average time separate 47.6 ns, chained 47.3 ns
Average time separate 46.7 ns, chained 47.2 ns
Average time separate 46.7 ns, chained 47.0 ns
Average time separate 46.0 ns, chained 46.6 ns
Average time separate 46.7 ns, chained 46.3 ns
使用 Java 7 更新 10
Average time separate 50.4 ns, chained 50.0 ns
Average time separate 50.1 ns, chained 50.1 ns
Average time separate 45.9 ns, chained 46.5 ns
Average time separate 46.6 ns, chained 46.7 ns
Average time separate 46.3 ns, chained 46.4 ns
Average time separate 46.7 ns, chained 46.5 ns
Average time separate 46.2 ns, chained 46.4 ns
Average time separate 46.6 ns, chained 46.0 ns
Average time separate 46.4 ns, chained 46.2 ns
Average time separate 45.9 ns, chained 46.2 ns
最初可能看起来有轻微的偏差,但如果您的运行更新 10 随着时间的推移没有明显的偏差。