|
The Java Specialists' Newsletter
Issue 070 2003-05-17
Category:
Performance
Java version: Too many dimensions are bad for youby Dr. Heinz M. Kabutz
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!).
Would you like to really understand Java concurrency? Join us for an
in-depth study of how threading works in Java. During the course,
you will learn how to write correct and fast multi-threaded Java code.
Please
click here if you would like to learn more. 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
Performance Articles
Related Java Course
Discuss at The Java Specialist Club
|