Usage of GapList.

80 views
Skip to first unread message

Bharat S

unread,
Feb 28, 2013, 7:34:45 AM2/28/13
to brownies-c...@googlegroups.com
Hi,

 I am trying to use GapList instead of ArrayList for the simple operation of adding a complex object. I see that arraylist is faster than GapList. Please see my code below. I have lot of instances of using these kind of things. Is there anyways that i could make this project faster.

GapList gapList = new GapList();
gapList.ensureCapacity(9000);
ComplexObject obj=null;
long before = System.currentTimeMillis();
   //System.out.println(System.currentTimeMillis());    
   for(int i=0;i<90000;i++){
    obj = new ComplexObject(i, "String1", new Double(i), new ArrayList(), null);
    gapList.addLast(obj);
   }
   
   long afterAdd =System.currentTimeMillis();
   System.out.println("Difference Gap List after add"+(afterAdd-before));
   for(int i=0;i<90000;i++){
        gapList.getLast();
   }
   
   long after =System.currentTimeMillis();
   System.out.println("Difference Gap List after remove "+(after-afterAdd));

System.out.println("----------------------------------------------------------");
 
   List arrayList =  new ArrayList();
   before = System.currentTimeMillis();    
   for(int i=0;i<90000;i++){
   // double j=Math.random();
    obj = new ComplexObject(i, "String1", new Double(i), new ArrayList(), null);
    arrayList.add(obj);
   }
   afterAdd =System.currentTimeMillis();
   System.out.println("Difference Gap List after add"+(afterAdd-before));
 
    
   for(int i=0;i<90000;i++){
    arrayList.get(i);
        }
//
   after =System.currentTimeMillis();
   System.out.println("Difference Array List aftre remove"+(after-afterAdd));

Thomas Mauch

unread,
Mar 3, 2013, 6:59:29 PM3/3/13
to brownies-c...@googlegroups.com
Hi Bharat

If you read the introducionary article about GapList, you will see that GapList is slighty slower than ArrayList if you just just add elements at the end, but it will outperform ArrayList in many other operations. So adding many thousands elements at the beginning will certainly cause performanc problems with an ArrayList, but with GapList you will not notice any difference to adding at the end.

Regards,
Thomas
Reply all
Reply to author
Forward
0 new messages