Advertisement
Guest User

TreeSet

a guest
Feb 15th, 2017
398
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.54 KB | None | 0 0
  1. package com.stackoverflow;
  2.  
  3. import org.openjdk.jmh.annotations.*;
  4. import org.openjdk.jmh.infra.Blackhole;
  5. import org.openjdk.jmh.runner.Runner;
  6. import org.openjdk.jmh.runner.RunnerException;
  7. import org.openjdk.jmh.runner.options.Options;
  8. import org.openjdk.jmh.runner.options.OptionsBuilder;
  9.  
  10. import java.io.FileNotFoundException;
  11. import java.io.IOException;
  12. import java.io.PrintStream;
  13. import java.util.*;
  14. import java.util.concurrent.ThreadLocalRandom;
  15. import java.util.stream.Collectors;
  16.  
  17. @Warmup(iterations = 3)
  18. @Measurement(iterations = 2)
  19. @State(Scope.Benchmark)
  20. @Fork(1)
  21. @BenchmarkMode(Mode.Throughput)
  22. public class StreamBenchmark5 {
  23.  
  24.     public static final int MAX_SIZE = 1_000_000;
  25.     public static final long ELEM_THRESHOLD = MAX_SIZE / 100;
  26.     private List<Foo> foos;
  27.  
  28.     @Setup
  29.     public void init() throws IOException, IllegalAccessException, InstantiationException {
  30.         foos = new ArrayList<>(MAX_SIZE);
  31.         for (int i = 0; i < MAX_SIZE; i++) {
  32.             foos.add(new Foo(ThreadLocalRandom.current().nextLong(ELEM_THRESHOLD)));
  33.         }
  34.     }
  35.  
  36.     @Benchmark
  37.        public void enhancedLoop_TreeSet(Blackhole bh) {
  38.         Set<MyObj> ret = new TreeSet<>();
  39.         for (Foo foo : foos)
  40.             ret.add(new MyObj(foo));
  41.         bh.consume(ret.toArray(new MyObj[ret.size()]));
  42.     }
  43.  
  44.     @Benchmark
  45.     public void simpleArray(Blackhole bh) {
  46.         MyObj[] ret = new MyObj[foos.size()];
  47.         for (int i=0;i<foos.size();i++)
  48.             ret[i] = new MyObj(foos.get(i));
  49.         Arrays.sort(ret);
  50.         int lastDistinct = 0;
  51.         for (int i = 1; i < ret.length; i++) {
  52.             if (ret[i].equals(ret[lastDistinct])) {
  53.                 continue;
  54.             }
  55.             lastDistinct++;
  56.             ret[lastDistinct] = ret[i];
  57.         }
  58.         MyObj[] ret2 = new MyObj[lastDistinct + 1];
  59.         System.arraycopy(ret, 0, ret2, 0, lastDistinct + 1);
  60.         bh.consume(ret2);
  61.     }
  62.  
  63.     @Benchmark
  64.     public void simpleStream(Blackhole bh) {
  65.         List<MyObj> ret = foos.stream().map(MyObj::new)
  66.                 .distinct().sorted()
  67.                 .collect(Collectors.toList());
  68.         bh.consume(ret.toArray(new MyObj[ret.size()]));
  69.     }
  70.  
  71.     @Benchmark
  72.     public void simpleStream_distinctAfterSort(Blackhole bh) {
  73.         List<MyObj> ret = foos.stream().map(MyObj::new)
  74.                 .sorted().distinct()
  75.                 .collect(Collectors.toList());
  76.         bh.consume(ret.toArray(new MyObj[ret.size()]));
  77.     }
  78.  
  79.     @Benchmark
  80.     public void treeSetStream(Blackhole bh) {
  81.         Set<MyObj> ret = foos.stream().map(MyObj::new)
  82.                 .collect(Collectors.toCollection(TreeSet::new));
  83.         bh.consume(ret.toArray(new MyObj[ret.size()]));
  84.     }
  85.  
  86.     public static void main(String[] args) throws Exception {
  87.         runTest();
  88.     }
  89.  
  90.     private static void runTest() throws FileNotFoundException, RunnerException {
  91.         PrintStream out = new PrintStream("log.txt");
  92. //        System.setOut(out);
  93.  
  94.         Options opt = new OptionsBuilder()
  95.                 .include(".*" + StreamBenchmark5.class.getSimpleName() + ".*")
  96.                 .forks(1)
  97.                 .build();
  98.         new Runner(opt).run();
  99.  
  100.         out.close();
  101.     }
  102.  
  103.     private static class Foo {
  104.         private long value;
  105.  
  106.         public Foo(long i) {
  107.             this.value = i;
  108.         }
  109.  
  110.         @Override
  111.         public boolean equals(Object o) {
  112.             if (this == o) return true;
  113.             if (o == null || getClass() != o.getClass()) return false;
  114.  
  115.             Foo foo = (Foo) o;
  116.  
  117.             if (value != foo.value) return false;
  118.  
  119.             return true;
  120.         }
  121.  
  122.         @Override
  123.         public int hashCode() {
  124.             return (int) (value ^ (value >>> 32));
  125.         }
  126.     }
  127.  
  128.     private static class MyObj implements Comparable<MyObj> {
  129.         private final Foo foo;
  130.  
  131.         public MyObj(Foo foo) {
  132.             this.foo = foo;
  133.         }
  134.  
  135.         @Override
  136.         public int compareTo(MyObj o) {
  137.             return Long.compare(foo.value, o.foo.value);
  138.         }
  139.  
  140.         @Override
  141.         public boolean equals(Object o) {
  142.             if (this == o) return true;
  143.             if (o == null || getClass() != o.getClass()) return false;
  144.  
  145.             MyObj myObj = (MyObj) o;
  146.  
  147.             if (foo != null ? !foo.equals(myObj.foo) : myObj.foo != null) return false;
  148.  
  149.             return true;
  150.         }
  151.  
  152.         @Override
  153.         public int hashCode() {
  154.             return foo != null ? foo.hashCode() : 0;
  155.         }
  156.     }
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement