Java Specialists' Java Training Europehome of the java specialists' newsletter

The Java Specialists' Newsletter
Issue 070b2003-05-18 Category: Performance Java version:

GitHub Subscribe Free RSS Feed

Multi-Dimensional Arrays - Creation Performance

by Dr. Heinz M. Kabutz

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

Performance Articles Related Java Course

Extreme Java - Concurrency and Performance for Java 8
Extreme Java - Advanced Topics for Java 8
Design Patterns
In-House Courses

© 2010-2016 Heinz Kabutz - All Rights Reserved Sitemap
Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners. JavaSpecialists.eu is not connected to Oracle, Inc. and is not sponsored by Oracle, Inc.