Creation Time Comparison of Multi Dimensional Array- Java Tutorials

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

Ads

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

Ads

 

We have 1000s of tutorials on our website. Search Tutorials tutorials on our website.

Share on Google+Share on Google+

Creation Time Comparison of Multi Dimensional Array- Java Tutorials

Posted on: April 18, 2011 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

 

Discuss: Multi-Dimensional Arrays - Creation Performance - Java Tutorials  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0

Ads

 

Ads