Skip to content

Instantly share code, notes, and snippets.

/IfVsArray.java Secret

Created September 29, 2015 11:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/cf21cb7da12ed3623c7e to your computer and use it in GitHub Desktop.
Save anonymous/cf21cb7da12ed3623c7e to your computer and use it in GitHub Desktop.
package misc;
import org.openjdk.jmh.annotations.*;
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;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.TimeUnit;
public class IfVsArray {
private static final char[] VOWELS = {'a', 'i', 'o', 'u', 'e'};
private static final Set<Character> VOWEL_SET = new HashSet<>();
private static final String VOWEL_STRING = "aeiou";
private static final String TEST_STRING = "the quick brown fox jumps over the lazy dog";
static {
Arrays.sort(VOWELS);
for(char c : VOWELS) {
VOWEL_SET.add(c);
}
}
@Benchmark
@BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@Warmup(iterations = 3)
@Measurement(iterations = 5)
public int withIf() {
int vowels = 0;
for(int i = 0;i < TEST_STRING.length();i++) {
char c = TEST_STRING.charAt(i);
if(c == 'a' || 'c' == 'e' || c == 'i' || c == 'o' || c == 'u') {
vowels++;
}
}
return vowels;
}
@Benchmark
@BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@Warmup(iterations = 3)
@Measurement(iterations = 5)
public int withArray() {
int vowels = 0;
for(int i = 0;i < TEST_STRING.length();i++) {
char c = TEST_STRING.charAt(i);
for(int j = 0;j < VOWELS.length;j++) {
if(VOWELS[j] == c) {
vowels++;
}
}
}
return vowels;
}
@Benchmark
@BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@Warmup(iterations = 3)
@Measurement(iterations = 5)
public int withHashSet() {
int vowels = 0;
for(int i = 0;i < TEST_STRING.length();i++) {
char c = TEST_STRING.charAt(i);
if(VOWEL_SET.contains(c)) {
vowels++;
}
}
return vowels;
}
@Benchmark
@BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@Warmup(iterations = 3)
@Measurement(iterations = 5)
public int withArrayBinSearch() {
int vowels = 0;
for(int i = 0;i < TEST_STRING.length();i++) {
char c = TEST_STRING.charAt(i);
if(Arrays.binarySearch(VOWELS, c) >= 0) {
vowels++;
}
}
return vowels;
}
@Benchmark
@BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@Warmup(iterations = 3)
@Measurement(iterations = 5)
public int withString() {
int vowels = 0;
for(int i = 0;i < TEST_STRING.length();i++) {
if(VOWEL_STRING.contains(TEST_STRING.substring(i, i + 1))) {
vowels++;
}
}
return vowels;
}
public static void main(final String[] args) throws RunnerException {
final Options opt = new OptionsBuilder()
.include(".*" + IfVsArray.class.getSimpleName() + ".*")
.forks(1)
.build();
new Runner(opt).run();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment