Running on Java 24-ea+25-3155 (Preview)
Home of The JavaSpecialists' Newsletter

070bMulti-Dimensional Arrays - Creation Performance

Author: Dr. Heinz M. KabutzDate: 2003-05-18Java Version: 1.4Category: Performance
 

Abstract: We have a look at the creation cost of multi-dimensional arrays in Java.

 

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 static final int ITERATIONS = 1000000;
  private static final int NUM_BINS = 20;
  private static final 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 https://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

 

Comments

We are always happy to receive comments from our readers. Feel free to send me a comment via email or discuss the newsletter in our JavaSpecialists Slack Channel (Get an invite here)

When you load these comments, you'll be connected to Disqus. Privacy Statement.

Related Articles

Browse the Newsletter Archive

About the Author

Heinz Kabutz Java Conference Speaker

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

Superpack '23

Superpack '24 Our entire Java Specialists Training in one huge bundle more...

Free Java Book

Dynamic Proxies in Java Book
Java Training

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

Java Consulting

We can help make your Java application run faster and trouble-shoot concurrency and performance bugs...