Skip to content

Instantly share code, notes, and snippets.

@matriv
Created March 1, 2017 09:23
Show Gist options
  • Save matriv/d6ad6e91350260950affe2bacf95a6e3 to your computer and use it in GitHub Desktop.
Save matriv/d6ad6e91350260950affe2bacf95a6e3 to your computer and use it in GitHub Desktop.
Benchmark Mode Cnt Score Error Units
MyBenchmark.testConcatenation thrpt 50 336375330.671 ± 2979060.438 ops/s
MyBenchmark.testFormat thrpt 50 1572940.434 ± 24415.477 ops/s
MyBenchmark.testStringBuilder thrpt 50 43651155.268 ± 1694489.589 ops/s
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.infra.Blackhole;
public class MyBenchmark {
private static final String TO_FORMAT = "This is a %s formatted string";
private static final String PREFIX = "This is a ";
private static final String SUFFIX = " formatted string";
@Benchmark
public void testFormat(Blackhole bh) {
bh.consume(String.format(TO_FORMAT, "myString"));
}
@Benchmark
public void testConcatenation(Blackhole bh) {
bh.consume(PREFIX + "myString" + SUFFIX);
}
@Benchmark
public void testStringBuilder(Blackhole bh) {
bh.consume(new StringBuilder().append(PREFIX).append("myString").append(SUFFIX).toString());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment