Translate

Wednesday, November 21, 2012

Java Performance in collections -01



USE APPROPRIATE COLLECTION

Array List Vs Linked List

ArrayList al = new ArrayList();
For(int i=0;i
{
          al.get(i);
}                                                                           Takes 281 ms
                            

LinkedList ll = new LinkedList();
For(int i=0;i
{
          ll.get(i);
}                                                                            Takes 5828 ms


Sequential access

ArrayList al = new ArrayList();
For(Iterator I = al,iterator();I,hasNext();)
{
          i.next();
}                                                                                     Takes 1375 ms


LinkedList ll = new LinkedList ();
For(Iterator I = ll,iterator();I,hasNext();)
{
          i.next();
}                                                                                     Takes 1047 ms


Note:- ArrayList is good for random access
          LinkedList is good for sequential access





Better in the best of both

ArrayList al = new ArrayList();
For(int I =0;i
{
          al.get(i);
}                                                                                     Takes 281 ms


LinkedList ll = new LinkedList ();
For(Iterator I = ll,iterator();I,hasNext();)
{
          i.next();
}                                                                                     Takes 1047 ms

Note:- Random access is better than sequential access

Insertion in ArrayList Vs LinkedList

ArrayList al = new ArrayList();
For(int I =0;i
{
          al.add(0, Integer.valueOf(i));
}                                                                                     Takes 328 ms

LinkedList ll = new LinkedList ();
For(int I =0;i
{
          ll.add(0, Integer.valueOf(i));
 }                                                                                    Takes 109 ms

Note:- LinkedList does insertion better than ArrayList





Vector vs. ArrayList

Vector is like to ArrayList but it is Synchronized version

Accession and Insertion in Vector Vs ArrayList


Random access

ArrayList al = new ArrayList();
For(int I =0;i
{
          al.get(i);
}                                                                                     Takes 281 ms


Vector vt = new Vector();
For(int I =0;i
{
          vt.get(i);
}                                                                                  Takes 422 ms

Sequential access

ArrayList al = new ArrayList();
For(Iterator I = al,iterator();I,hasNext();)
{
          i.next();
}                                                                                     Takes 1375 ms


Vector vt = new Vector();
For(Iterator i= vt,iterator();I,hasNext();)
{
          I,next();
}                                                                                     Takes 1890 ms




Insertion

ArrayList al = new ArrayList();
For(int I =0;i
{
          al.add(0, Integer.valueOf(i));
}                                                                                     Takes 328 ms

Vector vt = new Vector ();
For(int I =0;i
{
          vt.add(0, Integer.valueOf(i));
}                                                                                                          Takes 360 ms



Vector is slower than Array List in every method. Use Vector if only synchronize needed

Summary: java.util.List


No comments: