KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ListTest


1 import java.util.*;
2 import org.apache.avalon.excalibur.collections.VariableSizeBuffer;
3 import org.apache.avalon.excalibur.collections.FixedSizeBuffer;
4
5 public class ListTest
6 {
7  public static void main(String JavaDoc[] args)
8  {
9   int lInitialSize = Integer.parseInt(args[0]);
10   int lIterations = Integer.parseInt(args[1]);
11   ArrayList lArrayList = new ArrayList(lInitialSize + 1);
12   LinkedList lLinkedList = new LinkedList();
13   VariableSizeBuffer lVariableSizeBuffer = new VariableSizeBuffer(lInitialSize + 1);
14   FixedSizeBuffer lFixedSizeBuffer = new FixedSizeBuffer(lInitialSize + 1);
15   long lBegin, lEnd;
16
17   for (int i = 0; i < lInitialSize; i++)
18   {
19    lArrayList.add(new Integer JavaDoc(i));
20    lLinkedList.add(new Integer JavaDoc(i));
21    lVariableSizeBuffer.add(new Integer JavaDoc(i));
22    lFixedSizeBuffer.add(new Integer JavaDoc(i));
23   }
24
25   lBegin = System.currentTimeMillis();
26   for (int i = 0; i < lIterations; i++)
27   {
28    lArrayList.add(0, new Integer JavaDoc(i)); // Add to the head
29
lArrayList.remove(lInitialSize); // Remove from the tail
30
}
31   lEnd = System.currentTimeMillis();
32   System.out.println("Time: " + (lEnd - lBegin));
33
34   lBegin = System.currentTimeMillis();
35   for (int i = 0; i < lIterations; i++)
36   {
37    lLinkedList.addFirst(new Integer JavaDoc(i)); // Add to the head
38
lLinkedList.removeLast(); // Remove from the tail
39
}
40   lEnd = System.currentTimeMillis();
41   System.out.println("Time: " + (lEnd - lBegin));
42
43   lBegin = System.currentTimeMillis();
44   for (int i = 0; i < lIterations; i++)
45   {
46    lVariableSizeBuffer.add( new Integer JavaDoc(i) ); // Add to the head
47
lVariableSizeBuffer.remove(); // Remove from the tail
48
}
49   lEnd = System.currentTimeMillis();
50   System.out.println("Time: " + (lEnd - lBegin));
51
52   lBegin = System.currentTimeMillis();
53   for (int i = 0; i < lIterations; i++)
54   {
55    lFixedSizeBuffer.add( new Integer JavaDoc(i) ); // Add to the head
56
lFixedSizeBuffer.remove(); // Remove from the tail
57
}
58   lEnd = System.currentTimeMillis();
59   System.out.println("Time: " + (lEnd - lBegin));
60  }
61 }
62
Popular Tags