Skip to content

Instantly share code, notes, and snippets.

@u6f6o
Created July 20, 2017 08:06
Show Gist options
  • Select an option

  • Save u6f6o/d6ae7967b99e41bc881e2caf2842b015 to your computer and use it in GitHub Desktop.

Select an option

Save u6f6o/d6ae7967b99e41bc881e2caf2842b015 to your computer and use it in GitHub Desktop.
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.infra.Blackhole;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.concurrent.TimeUnit;
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
@Fork(5)
@State(Scope.Benchmark)
public class AnagramBenchmark {
private static final int WORDS_ARRAY_SIZE = 40727;
private String [] words;
@Setup
public void setup() {
try ( InputStream is = getClass().getResourceAsStream("/9-letter-words.txt");
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr)) {
words = new String[WORDS_ARRAY_SIZE];
for (int i = 0; ;i++) {
String line = br.readLine();
if (line == null) {
break;
}
words[i] = line;
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Benchmark
@OperationsPerInvocation(WORDS_ARRAY_SIZE - 1)
public void primsAnagram(Blackhole bh) {
for (int i = 0; i < (WORDS_ARRAY_SIZE - 1); i++) {
String s1 = words[i];
String s2 = words[i + 1];
bh.consume(Anagram.isAnagramUsingPrimes(s1, s2));
}
}
@Benchmark
@OperationsPerInvocation(WORDS_ARRAY_SIZE - 1)
public void arrayAnagram(Blackhole bh) {
for (int i = 0; i < (WORDS_ARRAY_SIZE - 1); i++) {
String s1 = words[i];
String s2 = words[i + 1];
bh.consume(Anagram.isAnagramUsingArray(s1, s2));
}
}
}
@u6f6o
Copy link
Copy Markdown
Author

u6f6o commented Jul 20, 2017

mvn clean install && nice -20 java -jar target/benchmarks.jar AnagramBenchmark -prof gc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment