Advertisement
Guest User

Untitled

a guest
May 31st, 2015
472
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.77 KB | None | 0 0
  1. package org.sample;
  2.  
  3. import org.openjdk.jmh.annotations.*;
  4. import org.openjdk.jmh.runner.Runner;
  5. import org.openjdk.jmh.runner.RunnerException;
  6. import org.openjdk.jmh.runner.options.Options;
  7. import org.openjdk.jmh.runner.options.OptionsBuilder;
  8.  
  9. import java.util.concurrent.TimeUnit;
  10.  
  11. @OutputTimeUnit(TimeUnit.SECONDS)
  12. @BenchmarkMode({Mode.Throughput})
  13. @Warmup(iterations = 10)
  14. @Fork(value = 1)
  15. @State(Scope.Benchmark)
  16. public class MyBenchmark {
  17.  
  18.     static class Queue {
  19.  
  20.         private final Object[] buffer;
  21.  
  22.         public int getCapacity() {
  23.             return buffer.length;
  24.         }
  25.  
  26.         Queue (int size) {
  27.             buffer = new Object[size];
  28.         }
  29.     }
  30.  
  31.     static class QueueCapacity {
  32.  
  33.         private final Object[] buffer;
  34.         private final int capacity;
  35.  
  36.         public int getCapacity() {
  37.             return capacity;
  38.         }
  39.  
  40.         QueueCapacity(int size) {
  41.             capacity = size;
  42.             buffer = new Object[size];
  43.         }
  44.     }
  45.  
  46.     public static int SIZE = 1_000_000;
  47.     public static int RUNS = 1_000_000;
  48.     public static Queue queue = new Queue(SIZE);
  49.     public static QueueCapacity queueCapacity = new QueueCapacity(SIZE);
  50.  
  51.     @Benchmark
  52.     public void testQueue() {
  53.         for (double i = 0; i < RUNS; i++) {
  54.             queue.getCapacity();
  55.         }
  56.     }
  57.  
  58.     @Benchmark
  59.     public void testQueueCapacity() {
  60.         for (double i = 0; i < RUNS; i++) {
  61.             queueCapacity.getCapacity();
  62.         }
  63.     }
  64.  
  65.     public static void main(String[] args) throws RunnerException {
  66.         Options opt = new OptionsBuilder()
  67.                 .include(MyBenchmark.class.getSimpleName())
  68.                 .forks(1)
  69.                 .build();
  70.         new Runner(opt).run();
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement