Skip to content

Instantly share code, notes, and snippets.

@vladimirdolzhenko
Last active November 11, 2016 12:10
Show Gist options
  • Save vladimirdolzhenko/b760b90cf58327248757a797a12dbae0 to your computer and use it in GitHub Desktop.
Save vladimirdolzhenko/b760b90cf58327248757a797a12dbae0 to your computer and use it in GitHub Desktop.
import java.util.concurrent.TimeUnit;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Threads;
import org.openjdk.jmh.annotations.Warmup;
import org.openjdk.jmh.infra.Blackhole;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
/**
* Benchmark (size) Mode Cnt Score Error Units EveragePerSingleString
* StringPerf.stringIntern 1 avgt 5 0.150 ± 0.009 us/op 0.15
* StringPerf.stringIntern 1000 avgt 5 169.744 ± 10.045 us/op 0.169744
* StringPerf.stringIntern 10000 avgt 5 1848.249 ± 42.983 us/op 0.1848249
* StringPerf.stringIntern 100000 avgt 5 27577.632 ± 1922.913 us/op 0.27577632
* StringPerf.stringIntern 1000000 avgt 5 773846.797 ± 66098.331 us/op 0.773846797
*
* @author vladimir.dolzhenko
* @since 2016-11-11
*/
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
@Fork(1)
@Warmup(iterations = 2, time = 1000, timeUnit = TimeUnit.MILLISECONDS)
@Measurement(iterations = 2, time = 1000, timeUnit = TimeUnit.MILLISECONDS)
@Threads(1)
public class StringPerf {
@State(Scope.Benchmark)
public static class BenchmarkState {
@Param( {"1", "1000", "10000", "100000", "1000000"})
public int size;
public String[] strings;
@Setup
public void setUp() {
strings = new String[size];
for (int i = 0; i < size; i++) {
strings[i] = "aaaaaaaaaa" + ('0' + i) + Integer.toString(i) + "zzzzzzzzzzzzz";
}
}
}
@Benchmark
public void stringIntern(BenchmarkState state, Blackhole bh) {
for (int i = 0; i < state.strings.length; i++) {
bh.consume(state.strings[i].intern());
}
}
public static void main(String[] args) throws RunnerException {
Options opt = new OptionsBuilder()
.include(StringPerf.class.getSimpleName())
.warmupIterations(5)
.measurementIterations(5)
.build();
new Runner(opt).run();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment