Home of The JavaSpecialists' Newsletter

070bMulti-Dimensional Arrays - Creation Performance

Posted: 2003-05-18Category: PerformanceJava Version: Dr. Heinz M. Kabutz
 

Abstract: 

 

One of our subscribers, Martin Schulte from Germany, sent me a correction for yesterday's newsletter, and I do not want to take too long before publishing it, since it is rather significant.

Martin discovered that the biggest difference in performance is the creation of the multi-dimensional arrays. Mark Grand also thought I should point out that there are no multi-dimensional arrays in Java, only arrays of arrays. This is the reason why we have a difference in performance when we create these arrays of arrays.

The difference in creation times can be seen in this small test example, I have left out the syntax highlighting to see your reaction ;-)

public class CreationTimes {
  private final static int ITERATIONS = 1000000;
  private final static int NUM_BINS = 20;
  private final static int THE_OTHER_DIM = 4;

  public static void main(String[] args) {
    testMultiArray();
    testMultiArray2();
    testSingleArray();
  }

  private static void testMultiArray() {
    long time = -System.currentTimeMillis();

    for (int repeat = 0; repeat < ITERATIONS; repeat++) {
      int[][] aTwoDim = new int[NUM_BINS][THE_OTHER_DIM];
    }

    time += System.currentTimeMillis();
    System.out.println("Time Elapsed for [][" + THE_OTHER_DIM + "] - " + 
                       time);
  }

  private static void testMultiArray2() {
    long time = -System.currentTimeMillis();

    for (int repeat = 0; repeat < ITERATIONS; repeat++) {
      int[][] aTwoDim = new int[THE_OTHER_DIM][NUM_BINS];
    }

    time += System.currentTimeMillis();
    System.out.println("Time Elapsed for [" + THE_OTHER_DIM + "][] - " + 
                       time);
  }

  private static void testSingleArray() {
    long time = -System.currentTimeMillis();

    for (int repeat = 0; repeat < ITERATIONS; repeat++) {
      int[] aOneDim = new int[NUM_BINS * THE_OTHER_DIM];
    }

    time += System.currentTimeMillis();
    System.out.println("Time Elapsed for [] - " + time);
  }
}

The result of running this code is:

Time Elapsed for [][4] - 9653
Time Elapsed for [4][] - 2394
Time Elapsed for [] - 671

On another note, I will be joining an elite group of authors on Bill Venners' Weblogs. The others on the list are really quite famous, so I am there to bring some balance :-) Check out the artima website on http://www.artima.com, I should be there within a week or so. I plan to use that forum as a supplement to my newsletter, and it will give you an opportunity to chat back publicly as well.

Kind regards

Heinz

 

Related Articles

Browse the Newsletter Archive

About the Author

demo

Java Champion, author of the Javaspecialists Newsletter, conference speaking regular... About Heinz

Java Training

We deliver relevant courses, by top Java developers to produce more resourceful and efficient programmers within their organisations.

Java Consulting

Nobody ever wants to call a Java performance consultant, but with first-hand experience repairing and improving commercial Java applications - JavaSpecialists are a good place to start...

Threading Emergency?

If your system is down, we will review it for 15 minutes and give you our findings for just 1 € without any obligation.