Skip to content

Instantly share code, notes, and snippets.

@vchimishuk
Created December 14, 2017 18:10
Show Gist options
  • Save vchimishuk/77fcb1e2b22a3d92a2a413a2878f767d to your computer and use it in GitHub Desktop.
Save vchimishuk/77fcb1e2b22a3d92a2a413a2878f767d to your computer and use it in GitHub Desktop.

Revisions

  1. vchimishuk created this gist Dec 14, 2017.
    61 changes: 61 additions & 0 deletions DequeBenchmark.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,61 @@
    package com.geomotiv.ads.benchmarks;

    import java.util.ArrayDeque;
    import java.util.LinkedList;
    import org.openjdk.jmh.annotations.Benchmark;
    import org.openjdk.jmh.annotations.Scope;
    import org.openjdk.jmh.annotations.State;
    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;

    @State(Scope.Benchmark)
    public class DequeueBenchmark {
    private static final long LIFETIME = 10;

    private final ArrayDeque<Long> arrayDeque;

    private final LinkedList<Long> linkedDeque;

    public DequeueBenchmark() {
    this.arrayDeque = new ArrayDeque<>();
    this.linkedDeque = new LinkedList<>();
    }

    @Benchmark
    public void arrayDequeBenchmark() {
    long now = System.currentTimeMillis();
    long expire = now - LIFETIME;

    arrayDeque.addLast(now);

    while (arrayDeque.getFirst() < expire) {
    arrayDeque.removeFirst();
    }
    }

    @Benchmark
    public void linkedDequeBenchmark() {
    long now = System.currentTimeMillis();
    long expire = now - LIFETIME;

    linkedDeque.addLast(now);

    while (linkedDeque.getFirst() < expire) {
    linkedDeque.removeFirst();
    }
    }

    public static void main(String[] args) throws RunnerException {
    Options opt = new OptionsBuilder()
    .include(DequeueBenchmark.class.getSimpleName())
    .warmupIterations(3)
    .measurementIterations(3)
    .measurementBatchSize(1_000_000)
    .threads(1)
    .forks(1)
    .build();
    new Runner(opt).run();
    }
    }