Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.sample;
- import org.openjdk.jmh.annotations.*;
- 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.util.concurrent.TimeUnit;
- @OutputTimeUnit(TimeUnit.SECONDS)
- @BenchmarkMode({Mode.Throughput})
- @Warmup(iterations = 10)
- @Fork(value = 1)
- @State(Scope.Benchmark)
- public class MyBenchmark {
- static class Queue {
- private final Object[] buffer;
- public int getCapacity() {
- return buffer.length;
- }
- Queue (int size) {
- buffer = new Object[size];
- }
- }
- static class QueueCapacity {
- private final Object[] buffer;
- private final int capacity;
- public int getCapacity() {
- return capacity;
- }
- QueueCapacity(int size) {
- capacity = size;
- buffer = new Object[size];
- }
- }
- public static int SIZE = 1_000_000;
- public static int RUNS = 1_000_000;
- public static Queue queue = new Queue(SIZE);
- public static QueueCapacity queueCapacity = new QueueCapacity(SIZE);
- @Benchmark
- public void testQueue() {
- for (double i = 0; i < RUNS; i++) {
- queue.getCapacity();
- }
- }
- @Benchmark
- public void testQueueCapacity() {
- for (double i = 0; i < RUNS; i++) {
- queueCapacity.getCapacity();
- }
- }
- public static void main(String[] args) throws RunnerException {
- Options opt = new OptionsBuilder()
- .include(MyBenchmark.class.getSimpleName())
- .forks(1)
- .build();
- new Runner(opt).run();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement