Monday, December 20, 2004

Java Generics collection add performance

I wanted to test the raw performance of using java generics with collections. So i wrote up the following code to add a similar nomber of objects to a collection.

public class Test {

private void testGenericsAddPerformance(){

long startTime = System.currentTimeMillis();
long endTime = 0;

System.out.println("STARTTime without generics ::: " + startTime);
ArrayList l = new ArrayList();

for(long i = 0;i<4000001>

l.add("SSSSSSSSSSSSSSSSSSSSSSSSSsss");

}
endTime = System.currentTimeMillis();
System.out.println("ENDTime without generics ::: " + endTime);
System.out.println("Diff without generics ::: " + (endTime-startTime) + " milliseconds");
l=null;
System.gc();

startTime = System.currentTimeMillis();
endTime = 0;

System.out.println("STARTTime with generics ::: " + startTime);
ArrayList l1 = new ArrayList();

for(long i = 0;i<4000001>

l1.add("SSSSSSSSSSSSSSSSSSSSSSSSSsss");

}
endTime = System.currentTimeMillis();
System.out.println("ENDTime with generics :::" + endTime);
System.out.println("Diffith generics ::: " + (endTime-startTime) + " milliseconds");
l=null;
System.gc();
}

public static void main(String[] args) {

Test this_ = new Test();
this_.testGenericsAddPerformance();

}
}


Here are the results.

STARTTime without generics ::: 1103546585484
ENDTime without generics ::: 1103546586921
Diff without generics ::: 1437 milliseconds

STARTTime with generics ::: 1103546586953
ENDTime with generics :::1103546587468
Diffith generics ::: 515 milliseconds

At least the initial raw performance seems to be better using Generics.

I have used JDK 1.5 version
1.5.0-beta2





0 Comments:

Post a Comment

<< Home