/**
*/

public final class MatrixCopyTest
{
    public void test(int numRows, int numCols, int iterations)
    {
        double[][] original = new double[numRows][numCols];

        for (int i=0; i<numRows; i++)
            for (int j=0; j<numCols; j++)
                original[i][j] = i+j;

        long startTime;
        long ebeDuration;
        long rowDuration;
        double change;

        // element by element copy
        startTime = System.currentTimeMillis();
        for (int count = 0; count<iterations; count++)
        {
            double[][] ebeCopy = new double[numRows][numCols];

            for (int i=0; i<numRows; i++)
                for (int j=0; j<numCols; j++)
                    ebeCopy[i][j] = original[i][j];

            ebeCopy = null;
        }
        ebeDuration = System.currentTimeMillis() - startTime;
        System.out.println("ebe " + ebeDuration);


        // row copy
        startTime = System.currentTimeMillis();
        for (int count = 0; count<iterations; count++)
        {
            double[][] rowCopy = new double[numRows][];
            double[] row;
            double[] originalRow;

            for (int i=0; i<numRows; i++)
            {
                originalRow = original[i];
                row = new double[numCols];
                for (int j=0; j<numCols; j++)
                    row[j] = originalRow[j];

                rowCopy[i] = row;
            }

            rowCopy = null;
            row = null;
        }
        rowDuration = System.currentTimeMillis() - startTime;
        change = (ebeDuration - rowDuration)*100.0;

        System.out.println("row " + rowDuration);
        System.out.println("improvement " + change/ebeDuration + " %");
        System.out.println("ebe takes " + change/rowDuration + " % longer");

    }

    public static void main(String[] args)
    {
        if (args.length != 3) System.err.println(
           "Wrong args, expecting: numRows, numColumns, numCopiesInTest");
        MatrixCopyTest mct = new MatrixCopyTest();
        mct.test(Integer.parseInt(args[0]), Integer.parseInt(args[1]),
                 Integer.parseInt(args[2]));
    }
}
