Created
July 20, 2017 08:06
-
-
Save u6f6o/d6ae7967b99e41bc881e2caf2842b015 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)); | |
| } | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
mvn clean install && nice -20 java -jar target/benchmarks.jar AnagramBenchmark -prof gc