Skip to content

Instantly share code, notes, and snippets.

@JosePaumard
Created December 4, 2019 09:04
Show Gist options
  • Save JosePaumard/ce73b5ee8b6adb7a89f2a2db4292aa0b to your computer and use it in GitHub Desktop.
Save JosePaumard/ce73b5ee8b6adb7a89f2a2db4292aa0b to your computer and use it in GitHub Desktop.
import org.eclipse.collections.api.stack.primitive.ImmutableCharStack;
import org.eclipse.collections.impl.block.factory.primitive.CharPredicates;
import org.eclipse.collections.impl.factory.Strings;
import org.eclipse.collections.impl.factory.primitive.CharStacks;
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.ArrayDeque;
import java.util.concurrent.TimeUnit;
import java.util.function.IntPredicate;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import static java.util.function.Predicate.not;
/*
* Benchmark result on "my machine"
* Benchmark Mode Cnt Score Error Units
* JLDDOne.EC_V1 avgt 9 1,333 ± 0,080 us/op
* JLDDOne.EC_V2 avgt 9 0,051 ± 0,001 us/op
* JLDDOne.Stream_V1 avgt 9 0,517 ± 0,024 us/op
* JLDDOne.Stream_V2 avgt 9 0,164 ± 0,020 us/op
*/
@SuppressWarnings("static-method")
@Warmup(iterations = 3, time = 1, timeUnit = TimeUnit.SECONDS)
@Measurement(iterations = 3, time = 1, timeUnit = TimeUnit.SECONDS)
@Fork(value = 3)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
@State(Scope.Benchmark)
public class JLDDOne {
Predicate<Character> isWhitespace = Character::isWhitespace;
Predicate<Character> isUpperCase = Character::isUpperCase;
Predicate<Character> isWhiteSpaceOrUpperCase = isWhitespace.or(isUpperCase);
Predicate<Character> isNotWhiteSpace = not(isWhitespace);
IntPredicate isWhiteSpaceInt = Character::isWhitespace;
IntPredicate isUpperCaseInt = Character::isUpperCase;
IntPredicate isWhiteSpaceOrUpperCaseInt = isWhiteSpaceInt.or(isUpperCaseInt);
IntPredicate isNotWhiteSpaceInt = isWhiteSpaceInt.negate();
@Benchmark
public char EC_V1() {
char foundEC = Strings.asChars("The Quick Brown Fox jumps over the Lazy Dog.")
.injectInto(CharStacks.immutable.empty(), ImmutableCharStack::push)
.asLazy()
.select(CharPredicates.or(Character::isUpperCase, Character::isWhitespace))
.collectChar(Character::toLowerCase)
.reject(Character::isWhitespace)
.detectIfNone(Character::isLetter, 'a');
return foundEC;
}
@Benchmark
public char EC_V2() {
char foundEC = Strings.asChars("The Quick Brown Fox jumps over the Lazy Dog.")
.asReversed()
.select(CharPredicates.or(Character::isUpperCase, Character::isWhitespace))
.collectChar(Character::toLowerCase)
.reject(Character::isWhitespace)
.detectIfNone(Character::isLetter, 'a');
return foundEC;
}
@Benchmark
public char Stream_V1() {
char foundWithStream = "The Quick Brown Fox jumps over the Lazy Dog.".chars()
.mapToObj(value -> (char) value)
.collect(Collectors.toCollection(ArrayDeque::new))
.stream()
.filter(isWhiteSpaceOrUpperCase)
.map(Character::toLowerCase)
.filter(isNotWhiteSpace)
.findFirst()
.orElse('a');
return foundWithStream;
}
@Benchmark
public char Stream_V2() {
char foundWithStream = (char) new StringBuilder("The Quick Brown Fox jumps over the Lazy Dog.")
.reverse().chars()
.filter(isWhiteSpaceOrUpperCaseInt)
.map(Character::toLowerCase)
.filter(isNotWhiteSpaceInt)
.findFirst()
.orElse('a');
return foundWithStream;
}
public static void main(String[] args) throws RunnerException {
Options opt = new OptionsBuilder()
.include(JLDDOne.class.getName())
.build();
new Runner(opt).run();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment