Skip to content

Instantly share code, notes, and snippets.

@amaembo
Created January 22, 2016 08:34
Show Gist options
  • Save amaembo/9ee163878311147665b6 to your computer and use it in GitHub Desktop.
Save amaembo/9ee163878311147665b6 to your computer and use it in GitHub Desktop.
package test;
import java.util.concurrent.TimeUnit;
import one.util.streamex.*;
import java.util.stream.*;
import java.util.function.*;
import java.util.*;
import java.math.*;
import org.openjdk.jmh.infra.Blackhole;
import org.openjdk.jmh.annotations.*;
@Warmup(iterations = 5, time = 500, timeUnit = TimeUnit.MILLISECONDS)
@Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
@Fork(3)
@State(Scope.Benchmark)
public class HeadTailTest {
public static <T, R> StreamEx<R> map(StreamEx<T> input, Function<T, R> mapper) {
return input.headTail((head, tail) -> map(tail, mapper).prepend(mapper.apply(head)));
}
List<String> input;
@Setup
public void setup() {
input = new Random(1).doubles(10000).mapToObj(String::valueOf).collect(Collectors.toList());
}
@Benchmark
public int jdkSimple() {
return input.stream().map(x -> x).collect(Collectors.summingInt(String::length));
}
@Benchmark
public List<String> jdkComplex() {
return input.stream().map(x -> x.replaceAll("[123]", "")).collect(Collectors.toList());
}
@Benchmark
public int htSimple() {
return map(StreamEx.of(input), x -> x).collect(Collectors.summingInt(String::length));
}
@Benchmark
public List<String> htComplex() {
return map(StreamEx.of(input), x -> x.replaceAll("[123]", "")).collect(Collectors.toList());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment