package com.github.nithril;

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.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * Created by nlabrot on 14/05/15.
 */
public class PackedObjectBench {

    public static final int NB = 10000;

    public static final Random RANDOM = new Random();


    @State(Scope.Benchmark)
    public static class ByteBufferState {

        WrappedPackedObject defaultAccessorPoint = new WrappedPackedObject();
        WrappedPackedObject2 defaultAccessorPoint2 = new WrappedPackedObject2();
        List<Integer> list;

        public ByteBufferState() {

            list = new ArrayList<>();

            for (int i = 0; i < NB; i++) {
                list.add(i);
            }

            defaultAccessorPoint.setBuffer(list);
            defaultAccessorPoint2.setBuffer(list);
        }

    }

    @Benchmark
    @BenchmarkMode(Mode.AverageTime)
    @Warmup(iterations = 2, time = 1)
    @Measurement(iterations = 2, time = 1)
    @OperationsPerInvocation(NB)
    @OutputTimeUnit(TimeUnit.NANOSECONDS)
    public int benchDefaultSum(ByteBufferState state) {

        int value = 0;

        AbstractPackedObject defaultAccessorPoint = state.defaultAccessorPoint;

        for (int i = 0; i < NB ; i ++) {
            defaultAccessorPoint.setIndex(i);
            value += defaultAccessorPoint.defaultSum();
        }

        return value;
    }

    @Benchmark
    @BenchmarkMode(Mode.AverageTime)
    @Warmup(iterations = 2, time = 1)
    @Measurement(iterations = 2, time = 1)
    @OperationsPerInvocation(NB)
    @OutputTimeUnit(TimeUnit.NANOSECONDS)
    public int benchSum(ByteBufferState state) {

        int value = 0;

        AbstractPackedObject defaultAccessorPoint = state.defaultAccessorPoint;

        for (int i = 0; i < NB ; i ++) {
            defaultAccessorPoint.setIndex(i);
            value += defaultAccessorPoint.sum();
        }

        return value;

    }

    @Benchmark
    @BenchmarkMode(Mode.AverageTime)
    @Warmup(iterations = 2, time = 1)
    @Measurement(iterations = 2, time = 1)
    @OperationsPerInvocation(NB)
    @OutputTimeUnit(TimeUnit.NANOSECONDS)
    public int benchSum2(ByteBufferState state) {

        int value = 0;

        AbstractPackedObject defaultAccessorPoint = state.defaultAccessorPoint2;

        for (int i = 0; i < NB ; i ++) {
            defaultAccessorPoint.setIndex(i);
            value += defaultAccessorPoint.sum();
        }

        return value;
    }



    @Benchmark
    @BenchmarkMode(Mode.AverageTime)
    @Warmup(iterations = 2, time = 1)
    @Measurement(iterations = 2, time = 1)
    @OperationsPerInvocation(NB)
    @OutputTimeUnit(TimeUnit.NANOSECONDS)
    public int benchInline(ByteBufferState state) {

        int value = 0;

        for (int i = 0; i < NB ; i ++) {
            value += state.list.get(i);
        }

        return value;
    }


    public static void main(String[] args) throws RunnerException {
        Options opt = new OptionsBuilder()
                .include(PackedObjectBench.class.getSimpleName())
                        //.addProfiler(LinuxPerfAsmProfiler.class)
                .forks(0)
                .build();

        new Runner(opt).run();
    }
}

@@ -302,33 +302,8 @@
 st;%0A
-import java.util.Random;%0A
 impo
@@ -336,16 +336,16 @@
 meUnit;%0A
+
 %0A/**%0A * 
@@ -459,64 +459,8 @@
 0;%0A%0A
-    public static final Random RANDOM = new Random();%0A%0A%0A
     
@@ -745,25 +745,24 @@
 erState() %7B%0A
-%0A
             
@@ -783,25 +783,24 @@
 ayList%3C%3E();%0A
-%0A
             
@@ -865,33 +865,32 @@
 ;%0A            %7D%0A
-%0A
             defa
@@ -1248,33 +1248,32 @@
 rState state) %7B%0A
-%0A
         int valu
@@ -1271,33 +1271,32 @@
  int value = 0;%0A
-%0A
         Abstract
@@ -1351,33 +1351,32 @@
 tAccessorPoint;%0A
-%0A
         for (int
@@ -1504,33 +1504,32 @@
 um();%0A        %7D%0A
-%0A
         return v
@@ -1790,33 +1790,32 @@
 rState state) %7B%0A
-%0A
         int valu
@@ -1813,33 +1813,32 @@
  int value = 0;%0A
-%0A
         Abstract
@@ -1901,17 +1901,16 @@
 rPoint;%0A
-%0A
         
@@ -2039,33 +2039,32 @@
 um();%0A        %7D%0A
-%0A
         return v
@@ -2069,17 +2069,16 @@
  value;%0A
-%0A
     %7D%0A%0A 
@@ -2326,33 +2326,32 @@
 rState state) %7B%0A
-%0A
         int valu
@@ -2349,33 +2349,32 @@
  int value = 0;%0A
-%0A
         Abstract
@@ -2438,17 +2438,16 @@
 Point2;%0A
-%0A
         
@@ -2576,33 +2576,32 @@
 um();%0A        %7D%0A
-%0A
         return v
@@ -2875,17 +2875,16 @@
 tate) %7B%0A
-%0A
         
@@ -2898,17 +2898,16 @@
 ue = 0;%0A
-%0A
         
@@ -2975,24 +2975,24 @@
 ist.get(i);%0A
+
         %7D%0A%0A 
@@ -2985,25 +2985,24 @@
 ;%0A        %7D%0A
-%0A
         retu

Replace bytebuffer with an arraylist