The Source for Java Technology Collaboration
User: Password:



Start New Message Delete Post a Reply

Article: 
 Boxing Conversion in J2SE 5.0
Subject:  The performance comparision is not correct
Date:  2007-01-07 16:55:48
From:  jianwu_chen


The example you used for performance comparison is not suitable in this case. As List provides more functionality than int[]. In some case, you have to use List because of the extra futures. In that case, you can't avoid wrapping a primitive type.

Here actually you compare the array with primitive type and list with objects wrapping with primitive type. That's why the result is so much different.

Here I modified a bit on your example: you will see the result compare with auto-boxing and manual boxing:

package test;
import java.util.*;

public class AutoBoxingPerformanceTest{
public static void main(String args[]){
long time1 = 0;
long time2 = 0;
List<Integer> listValues = new ArrayList<Integer>();
int arrValues[] = new int[1000000];
/* Inserting values into List and Array */
for(int i =0;i<1000000;i++){
listValues.add(i);
arrValues[i]=i;
}
/* Reterive the values from collection objects and do the multiplication*/
time1 = System.currentTimeMillis();
for(int i=0;i<1000000;i++){
listValues.set(i,listValues.get(i)*10);
}
time2 = System.currentTimeMillis();
System.out.println("AutoBoxing with Collection : "+(time2-time1)+"ms");


time1 = System.currentTimeMillis();
for(int i=0;i<1000000;i++){
listValues.set(i, new Integer(listValues.get(i).intValue()*10));
}
time2 = System.currentTimeMillis();
System.out.println("Manual with Collection : "+(time2-time1)+"ms");



/* Reterive the values from arrays and do the multiplication*/
time1 = System.currentTimeMillis();
for(int i=0;i<1000000;i++){
arrValues[i]=arrValues[i]*10;
}
time2 = System.currentTimeMillis();
System.out.println("Using an Array : "+(time2-time1)+"ms");
}
}

In my slow machine, the result is:
AutoBoxing with Collection : 1151ms
Manual Boxing with Collection : 962ms
Using an Array : 70ms

The auto boxing is slightly slower than manual boxing.

 Feed java.net RSS Feeds