Abstract: Multi-dimensional arrays in Java are simply arrays of arrays. This can have consequences in CPU and memory performance.
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!).
javaspecialists.teachable.com: Please visit our new self-study course catalog to see how you can upskill your Java knowledge.
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 static final int NUM_BINS = 1000; private static final 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
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)
We deliver relevant courses, by top Java developers to produce more resourceful and efficient programmers within their organisations.