Skip to content

Instantly share code, notes, and snippets.

@eleco
Created June 30, 2015 08:44
Show Gist options
  • Save eleco/d4096caa751eda96bf8f to your computer and use it in GitHub Desktop.
Save eleco/d4096caa751eda96bf8f to your computer and use it in GitHub Desktop.
string intern benchmark with JMH
package jmh;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.profile.GCProfiler;
import org.openjdk.jmh.profile.StackProfiler;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
@State(Scope.Thread)
public
class StringInternTest {
private static final int batch_size = 300_000;
private List<String> lst = new ArrayList();
@Setup(Level.Iteration)
public void setup(){
lst.clear();
}
@Benchmark
public List<String> withoutStringIntern() {
lst.add(String.valueOf(lst.size() % 100));
return lst;
}
@Benchmark
public List<String> withStringIntern() {
lst.add(String.valueOf(lst.size() % 100).intern());
return lst;
}
public static void main(String ...args) throws Exception {
Options opts = new OptionsBuilder()
.include(StringInternTest.class.getSimpleName())
.forks(1)
.warmupIterations(5)
.warmupBatchSize(batch_size)
.mode(Mode.SingleShotTime)
.timeUnit(TimeUnit.MILLISECONDS)
.measurementIterations(20)
.measurementBatchSize(batch_size)
.addProfiler(GCProfiler.class)
.addProfiler(StackProfiler.class)
.jvmArgs("-server", "-Xmx128m", "-Xms128m")
.build();
new Runner(opts).run();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment