Home of The JavaSpecialists' Newsletter

070Too many dimensions are bad for you

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

Abstract: 

 

Welcome to the 70th edition of The Java(tm) Specialists' Newsletter, where we have a quick look at multi-dimensional arrays. Many articles are available about this problem, so in this newsletter, I will briefly point out the problem and give you one example. I would like to thank Roy Emmerich (Peralex in Cape Town) and Pieter Potgieter and Louis Pool from Grintek Ewation for reminding me of this problem and for sending me a test program that illustrated it.

This past week we had a Design Patterns Course at one of the finest hotels of Cape Town. OK, it is *renowned* to be one of the finest ;-) The lunches were fantastic, but the coffees, well! Let me say that they are probably not used to having programmers at their hotel? There is a direct correlation between quality of coffee and quality of code, so we are quite discerning when it comes to the magic brew. But the lunches more than made up for it! On the first day we had baked sirloin steak, on the second we had filled chicken breasts and on the third we were treated to excellently prepared line fish (Cape Salmon, otherwise known as Geelbek). The discussions about the Design Patterns were deep, and we all left thoroughly exhausted (especially me!).

NEW: Refactoring to Java 8 Lambdas and Streams Workshop Are you currently using Java 6 or 7 and would like to see how Java 8 can improve your code base? Are you tired of courses that teach you a whole bunch of techniques that you cannot apply in your world? Check out our one day intensive Refactoring to Java 8 Lambdas and Streams Workshop.

Too many dimensions are bad for you

Something you learn in Java nursery school is to avoid multi-dimensional arrays like the plague. An array is an object in its own right, and when you have multi-dimensional arrays, you have arrays of objects. Navigating these will take considerable processing power. Instead of making a multi-dimensional array of size n by m, rather make a one-dimensional array of size n times m and then do the index calculation yourself.

public class MultiDimensions {
  private final static int NUM_BINS = 1000;
  private final static int ITERATIONS = 10000;

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

  private static void testMultiArray() {
    long time = -System.currentTimeMillis();
    // just making sure that the number of operations is equal
    int ops = 0;

    for (int repeat = 0; repeat < ITERATIONS; repeat++) {
      int[][] aTwoDim = new int[NUM_BINS][4];
      for (int i = 0; i < aTwoDim.length; i++) {
        for (int j = 0; j < aTwoDim[i].length; j++) {
          ops++;
          aTwoDim[i][j] = j;
        }
      }
    }

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

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

    for (int repeat = 0; repeat < ITERATIONS; repeat++) {
      int[][] aTwoDim = new int[4][NUM_BINS];
      for (int i = 0; i < aTwoDim.length; i++) {
        for (int j = 0; j < aTwoDim[i].length; j++) {
          ops++;
          aTwoDim[i][j] = j;
        }
      }
    }

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

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

    for (int repeat = 0; repeat < ITERATIONS; repeat++) {
      int[] aOneDim = new int[NUM_BINS * 4];
      for (int i = 0; i < aOneDim.length/4; i++) {
        for (int j = 0; j < 4; j++) {
          ops++;
          aOneDim[i*4 + j] = j;
        }
      }
    }

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

When I run this code with the client hotspot under JDK 1.4.1, I get the following results:

40000000
Time Elapsed for [][4] - 4226
40000000
Time Elapsed for [4][] - 631
40000000
Time Elapsed for [] - 671

The server hotspot fares slightly better:

40000000
Time Elapsed for [][4] - 3675
40000000
Time Elapsed for [4][] - 350
40000000
Time Elapsed for [] - 561

The results are enlightning. If you have an array with a big first index, you will be better off using a single-dimensional array. Under server hotspot, the multi-dimensional array with a small first index fares quite a bit better than the single-dimensional array.

That's all that I would like to say about this issue. I warned you that this newsletter would be short :-)

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.