I have a microbenchmark that shows very strange results:
@BenchmarkMode(Mode.Throughput)
@Fork(1)
@State(Scope.Thread)
@Warmup(iterations = 10, time = 1, timeUnit = TimeUnit.SECONDS, batchSize = 1000)
@Measurement(iterations = 40, time = 1, timeUnit = TimeUnit.SECONDS, batchSize = 1000)
public class Chaining {
private String a1 = "111111111111111111111111";
private String a2 = "222222222222222222222222";
private String a3 = "333333333333333333333333";
@Benchmark
public String typicalChaining() {
return new StringBuilder().append(a1).append(a2).append(a3).toString();
}
@Benchmark
public String noChaining() {
StringBuilder sb = new StringBuilder();
sb.append(a1);
sb.append(a2);
sb.append(a3);
return sb.toString();
}
}
I'm expecting the results of both tests to be the same or at least very close. However, the difference is almost 5x:
# Run complete. Total time: 00:01:41
Benchmark Mode Cnt Score Error Units
Chaining.noChaining thrpt 40 8538.236 ± 209.924 ops/s
Chaining.typicalChaining thrpt 40 36729.523 ± 988.936 ops/s
Does anybody know how that is possible?
-Djava.compiler=NONE
, by the way). Results are close to identical for each benchmark (and much slower). – Fatedfinal
, but I don't know if the compiler can still spot that they are never reassigned). If it was me I would test with strings that are dynamically built at runtime in a way such that the compiler is not able to infer their values at compile time, just to be safer. – Isfahan