KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > StackTest


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