【实现】
public static void main(String[] args) {
int opCount = 100000;
ArrayStack<Integer> arrayStack = new ArrayStack<>();
LinkedListStack<Integer> linkedListStack = new LinkedListStack<>();
double time1 = testStack(arrayStack,opCount);
double time2 = testStack(linkedListStack,opCount);
System.out.println( time1 +"\n" + time2);
}
public static double testStack(Stack<Integer> stack,int opCount){
Random random = new Random();
long start = System.nanoTime();
for (int i = 0; i < opCount; i++) {
stack.push(random.nextInt(Integer.MAX_VALUE));
}
for (int i = 0; i < opCount; i++) {
stack.pop();
}
long end = System.nanoTime();
return (end - start)/1000000000.0;
}
结果
ArrayStack:0.0111005
LinkedListStack:0.0083588