In this section, we will compare the creation time between the different size/dimension of array.
As you know that multidimensional array is the array of arrays. Practically it doesn't really possess dimensions rather it is array of arrays.
In the given below example, you can see the difference in the creation time :
package simpleCoreJava;
public class MultiDimArrayCreateTime {
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 output of the above code shows the creation time of each array, which indicates that
| Time Elapsed for [][4] - 2875 Time Elapsed for [4][] - 875 Time Elapsed for [] - 250 |
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.
Ask Questions? Discuss: Multi-Dimensional Arrays - Creation Performance - Java Tutorials
Post your Comment