Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package com.codelexems;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
@State(Scope.Thread)
@BenchmarkMode(Mode.AverageTime)
public class StringEqualsBenchmark {
String s1;
String s2;
@Setup
public void setup() throws Exception{
s1 = getRandom();
s2 = getRandom();
}
String getRandom(){
Random random = new Random();
IntStream firstStream = random.ints('a', 'z' + 1).limit(10_000);
IntStream secondStream = random.ints('A', 'Z' + 1).limit(10_000);
List<Integer> list = IntStream.concat(firstStream,secondStream).boxed()
.collect(Collectors.collectingAndThen(Collectors.toList(),l->{Collections.shuffle(l); return l;}));
StringBuilder stringBuilder = new StringBuilder();
for (int anInt : list) {
stringBuilder.append((char)anInt);
}
return stringBuilder.toString();
}
@Benchmark
@OutputTimeUnit(TimeUnit.MICROSECONDS)
public boolean withLowerCase(){
return s1.toLowerCase().equals(s2.toLowerCase());
}
@Benchmark
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public boolean withIgnoreCases(){
return s1.equalsIgnoreCase(s2);
}
public static void main(String[] args) throws Exception {
Options options = new OptionsBuilder()
.include(StringEqualsBenchmark.class.getSimpleName())
.warmupIterations(3)
.measurementIterations(10)
.forks(1)
.build();
Runner runner = new Runner(options);
runner.run();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment