Skip to content

Instantly share code, notes, and snippets.

@ejain
Created December 10, 2014 00:52

Revisions

  1. ejain created this gist Dec 10, 2014.
    43 changes: 43 additions & 0 deletions ArithmeticBenchmark.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@
    package benchmark;

    import org.openjdk.jmh.annotations.Benchmark;
    import org.openjdk.jmh.annotations.Mode;
    import org.openjdk.jmh.annotations.Scope;
    import org.openjdk.jmh.annotations.State;
    import org.openjdk.jmh.runner.Runner;
    import org.openjdk.jmh.runner.options.Options;
    import org.openjdk.jmh.runner.options.OptionsBuilder;

    @State(Scope.Thread)
    public class ArithmeticBenchmark {

    @Benchmark
    public long testDivide() {
    long value = Long.MAX_VALUE;
    while (value > 0) {
    value /= 2;
    }
    return value;
    }

    @Benchmark
    public long testShift() {
    long value = Long.MAX_VALUE;
    while (value > 0) {
    value = value >>> 1;
    }
    return value;
    }

    public static void main(String... args) throws Exception {
    Options opts = new OptionsBuilder()
    .include(ArithmeticBenchmark.class.getSimpleName())
    .mode(Mode.Throughput)
    .warmupIterations(10)
    .measurementIterations(10)
    .jvmArgs("-server")
    .forks(1)
    .build();
    new Runner(opts).run();
    }
    }
    3 changes: 3 additions & 0 deletions results.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    Benchmark Mode Samples Score Error Units
    b.ArithmeticBenchmark.testDivide thrpt 10 3695399.946 ± 87327.372 ops/s
    b.ArithmeticBenchmark.testShift thrpt 10 8091063.504 ± 114824.341 ops/s