Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.stackoverflow;
- import org.openjdk.jmh.annotations.*;
- import org.openjdk.jmh.infra.Blackhole;
- 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.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.PrintStream;
- import java.util.*;
- import java.util.concurrent.ThreadLocalRandom;
- import java.util.stream.Collectors;
- @Warmup(iterations = 3)
- @Measurement(iterations = 2)
- @State(Scope.Benchmark)
- @Fork(1)
- @BenchmarkMode(Mode.Throughput)
- public class StreamBenchmark5 {
- public static final int MAX_SIZE = 1_000_000;
- public static final long ELEM_THRESHOLD = MAX_SIZE / 100;
- private List<Foo> foos;
- @Setup
- public void init() throws IOException, IllegalAccessException, InstantiationException {
- foos = new ArrayList<>(MAX_SIZE);
- for (int i = 0; i < MAX_SIZE; i++) {
- foos.add(new Foo(ThreadLocalRandom.current().nextLong(ELEM_THRESHOLD)));
- }
- }
- @Benchmark
- public void enhancedLoop_TreeSet(Blackhole bh) {
- Set<MyObj> ret = new TreeSet<>();
- for (Foo foo : foos)
- ret.add(new MyObj(foo));
- bh.consume(ret.toArray(new MyObj[ret.size()]));
- }
- @Benchmark
- public void simpleArray(Blackhole bh) {
- MyObj[] ret = new MyObj[foos.size()];
- for (int i=0;i<foos.size();i++)
- ret[i] = new MyObj(foos.get(i));
- Arrays.sort(ret);
- int lastDistinct = 0;
- for (int i = 1; i < ret.length; i++) {
- if (ret[i].equals(ret[lastDistinct])) {
- continue;
- }
- lastDistinct++;
- ret[lastDistinct] = ret[i];
- }
- MyObj[] ret2 = new MyObj[lastDistinct + 1];
- System.arraycopy(ret, 0, ret2, 0, lastDistinct + 1);
- bh.consume(ret2);
- }
- @Benchmark
- public void simpleStream(Blackhole bh) {
- List<MyObj> ret = foos.stream().map(MyObj::new)
- .distinct().sorted()
- .collect(Collectors.toList());
- bh.consume(ret.toArray(new MyObj[ret.size()]));
- }
- @Benchmark
- public void simpleStream_distinctAfterSort(Blackhole bh) {
- List<MyObj> ret = foos.stream().map(MyObj::new)
- .sorted().distinct()
- .collect(Collectors.toList());
- bh.consume(ret.toArray(new MyObj[ret.size()]));
- }
- @Benchmark
- public void treeSetStream(Blackhole bh) {
- Set<MyObj> ret = foos.stream().map(MyObj::new)
- .collect(Collectors.toCollection(TreeSet::new));
- bh.consume(ret.toArray(new MyObj[ret.size()]));
- }
- public static void main(String[] args) throws Exception {
- runTest();
- }
- private static void runTest() throws FileNotFoundException, RunnerException {
- PrintStream out = new PrintStream("log.txt");
- // System.setOut(out);
- Options opt = new OptionsBuilder()
- .include(".*" + StreamBenchmark5.class.getSimpleName() + ".*")
- .forks(1)
- .build();
- new Runner(opt).run();
- out.close();
- }
- private static class Foo {
- private long value;
- public Foo(long i) {
- this.value = i;
- }
- @Override
- public boolean equals(Object o) {
- if (this == o) return true;
- if (o == null || getClass() != o.getClass()) return false;
- Foo foo = (Foo) o;
- if (value != foo.value) return false;
- return true;
- }
- @Override
- public int hashCode() {
- return (int) (value ^ (value >>> 32));
- }
- }
- private static class MyObj implements Comparable<MyObj> {
- private final Foo foo;
- public MyObj(Foo foo) {
- this.foo = foo;
- }
- @Override
- public int compareTo(MyObj o) {
- return Long.compare(foo.value, o.foo.value);
- }
- @Override
- public boolean equals(Object o) {
- if (this == o) return true;
- if (o == null || getClass() != o.getClass()) return false;
- MyObj myObj = (MyObj) o;
- if (foo != null ? !foo.equals(myObj.foo) : myObj.foo != null) return false;
- return true;
- }
- @Override
- public int hashCode() {
- return foo != null ? foo.hashCode() : 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement