2

I read an article in stackoverflow posted 4 years ago, see here: Fastest way to loop through a 2d array? Almost every answer agreed that scan horizontally will faster. I wrote a short Java program to check this, it turned out not the case. I choose 400x400 matrix. The time for scan horizontally is 6 and the time for scan vertically is 3. I checked other sizes of matrix. It also turned out scan vertically is faster. Do I miss something or is it indeed the case?

public class Test {

public static void main(String[] args) {

        int row=Integer.parseInt(args[0]);
    int column=Integer.parseInt(args[1]);
    int[][] bigarray=new int[row][column];

    long startTime = System.currentTimeMillis();
    for(int i=0;i<row;i++)
        for(int j=0;j<column;j++)
            bigarray[i][j]=Math.abs(i-j)-Math.abs(i-j);


    long endTime   = System.currentTimeMillis();
    long totalTime = endTime - startTime;
    System.out.println("scan horizentally time is: ");
    System.out.println(totalTime);

    int[][] bigarray1=new int[row][column];

    long startTime1 = System.currentTimeMillis();
    for(int j=0;j<column;j++)
        for(int i=0;i<row;i++)
            bigarray1[i][j]=Math.abs(i-j)-Math.abs(i-j);


    long endTime1   = System.currentTimeMillis();
    long totalTime1 = endTime1 - startTime1;
    System.out.println("scan vertically time is: ");
    System.out.println(totalTime1);

}

}
Community
  • 1
  • 1
ohmygoddess
  • 619
  • 1
  • 7
  • 23
  • 2
    First, to measure time, you should use [`System.nanoTime`](http://docs.oracle.com/javase/7/docs/api/java/lang/System.html#nanoTime()) instead `System.currentTimeMillis` since the former is more accurate. Second, refer to this link to have a better understanding about micro benchmarking: http://stackoverflow.com/q/504103/1065197 – Luiggi Mendoza Nov 22 '13 at 21:50
  • I am a bit curious about your results with large matrices; 400x400 is in favor of vertically on my machine, but the advantage slips rapidly: 4000x4000 is a very different story. – ljgw Nov 22 '13 at 21:59
  • Yes, if I check 4000x4000 or bigger matrix, then the horizontal scan is faster. That's very interesting! – ohmygoddess Nov 22 '13 at 22:20
  • I changed the code by nano time, the result is similar to before. For small or median size matrix, horizontal scan is slower. The situation changes if use big enough matrix. – ohmygoddess Nov 22 '13 at 22:26
  • swap the cases in the code and check if you get the same result? – Stefan Haustein Nov 23 '13 at 00:02

1 Answers1

0

For the horizontal version, you could optimize the code:

for(int i=0;i<row;i++)
    int[] rowArray = bigarray[i];
    for(int j=0;j<column;j++)
        rowArray[j]=Math.abs(i-j)-Math.abs(i-j);

I wouldn't be surprised if the first test is always slower with your test setup. Java takes a lot of warmup time... A better test setup might be to have two separate programs, and to take a few warmup loops before taking the time...

Stefan Haustein
  • 18,427
  • 3
  • 36
  • 51