// java -XX:CompileCommand=compileonly,TestOopCopy::copy* -XX:CompileCommand=printcompilation,TestOopCopy::copy* -Xbatch TestOopCopy.java

public class TestOopCopy {
    public static int size = 100_000;

    public interface Benchmark {
        public void call();
    }

    public static void main(String[] args) {
        A[] a = new A[size];
        A[] b = new A[size];

        for (int i = 0; i < a.length; i++) {
            a[i] = new A(i, -i);
        }

        benchmark(() -> copy_A1(a, b));
        benchmark(() -> copy_A2(a, b));
    }

    public static void benchmark(Benchmark b) {
        System.out.println("Warmup");
        for (int i = 0; i < 10_000; i++) {
            b.call();
        }

        System.out.println("Run");
        long t0 = System.nanoTime();
        for (int i = 0; i < 50_000; i++) {
            b.call();
        }
        long t1 = System.nanoTime();
        System.out.println("elapsed: " + (t1 - t0) * 1e-9f);
    }

    public static void copy_A1(A[] a, A b[]) {
        for (int i = 0; i < a.length; i++) {
            b[i] = a[i];
        }
    }

    public static void copy_A2(A[] a, A b[]) {
        System.arraycopy(a, 0, b, 0, a.length);
    }

    public static class A {
        public int v0;
        public int v1;

        public A(int v0, int v1) {
            this.v0 = v0;
            this.v1 = v1;
	}
    }
}
