Advertisement
Guest User

Untitled

a guest
Aug 29th, 2016
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.02 KB | None | 0 0
  1. package com.stackoverflow;
  2.  
  3.  
  4. import org.openjdk.jmh.annotations.*;
  5. import org.openjdk.jmh.infra.Blackhole;
  6. import org.openjdk.jmh.runner.Runner;
  7. import org.openjdk.jmh.runner.RunnerException;
  8. import org.openjdk.jmh.runner.options.Options;
  9. import org.openjdk.jmh.runner.options.OptionsBuilder;
  10.  
  11. import java.io.FileNotFoundException;
  12. import java.io.IOException;
  13. import java.util.Comparator;
  14. import java.util.PriorityQueue;
  15. import java.util.Random;
  16.  
  17. @Warmup(iterations = 5)
  18. @Measurement(iterations = 2)
  19. @State(Scope.Benchmark)
  20. @Fork(1)
  21. @BenchmarkMode(Mode.Throughput)
  22. @Threads(1)
  23. public class CounterBenchmark {
  24.  
  25.     private static final int numberExamples = 10000;
  26.     private static final int numberNeighbours = 10;
  27.  
  28.     private double sampleData[];
  29.  
  30.     @Setup
  31.     public void init() throws IOException, IllegalAccessException, InstantiationException {
  32.         Random rnd = new Random();
  33.         sampleData = new double[numberExamples];
  34.         for (int i = 0; i < numberExamples; i++) {
  35.             sampleData[i] = rnd.nextDouble();
  36.         }
  37.     }
  38.  
  39.     @Benchmark
  40.     public void testHeap(Blackhole bh) {
  41.         final double[] scores = sampleData;
  42.  
  43.         PriorityQueue<Integer> myHeap = new PriorityQueue<>(numberNeighbours, new Comparator<Integer>() {
  44.             @Override
  45.             public int compare(Integer o1, Integer o2) {
  46.                 return -Double.compare(scores[o1], scores[o2]);
  47.             }
  48.         });
  49.  
  50.         int top;
  51.         for (int i = 0; i < numberExamples; i++) {
  52.             if (i < numberNeighbours) {
  53.                 myHeap.offer(i);
  54.             } else {
  55.                 top = myHeap.peek();
  56.                 if (scores[top] > scores[i]) {
  57.                     myHeap.poll();
  58.                     myHeap.offer(i);
  59.                 }
  60.             }
  61.         }
  62.         bh.consume(myHeap);
  63.     }
  64.  
  65.     @Benchmark
  66.     public void testSimple(Blackhole bh) {
  67.         final double[] scores = sampleData;
  68.         int[] candidates = new int[numberNeighbours];
  69.         int top = 0;
  70.         for (int i = 0; i < numberExamples; i++) {
  71.             if (i < numberNeighbours) {
  72.                 candidates[i] = i;
  73.                 if (scores[candidates[top]] < scores[candidates[i]]) top = i;
  74.             } else {
  75.                 if (scores[candidates[top]] > scores[i]) {
  76.                     candidates[top] = i;
  77.                     top = 0;
  78.                     for (int j = 1; j < numberNeighbours; j++) {
  79.                         if (scores[candidates[top]] < scores[candidates[j]]) top = j;
  80.                     }
  81.                 }
  82.             }
  83.         }
  84.         bh.consume(candidates);
  85.     }
  86.  
  87.     public static void main(String[] args) throws Exception {
  88.         runTest();
  89.     }
  90.  
  91.     private static void runTest() throws FileNotFoundException, RunnerException {
  92.         Options opt = new OptionsBuilder()
  93.                 .include(".*" + CounterBenchmark.class.getSimpleName() + ".*")
  94.                 .forks(1)
  95.                 .build();
  96.         new Runner(opt).run();
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement