Home Javatutorials Creation Time Comparison of Multi Dimensional Array- Java Tutorials
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Creation Time Comparison of Multi Dimensional Array- Java Tutorials

Advertisement
This page discusses - Creation Time Comparison of Multi Dimensional Array - Java Tutorials

Creation Time Comparison of Multi Dimensional Array

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);
}
}

Output :

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
Advertisements

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 18, 2011

Ask Questions?    Discuss: Multi-Dimensional Arrays - Creation Performance - Java Tutorials  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com