【实现】
public class Main {
public static void main(String[] args) {
int opCount = 100000;
ArrayQueue<Integer> arrayQueue = new ArrayQueue<>();
LinkedListQueue<Integer> linkedListQueue = new LinkedListQueue<>();
double time1 = testStack(arrayQueue,opCount);
double time2 = testStack(linkedListQueue,opCount);
System.out.println( "ArrayStack:" + time1 +"\n" + "LinkedListStack:"+time2);
}
public static double testStack(Queue<Integer> stack, int opCount){
Random random = new Random();
long start = System.nanoTime();
for (int i = 0; i < opCount; i++) {
stack.enqueue(random.nextInt(Integer.MAX_VALUE));
}
for (int i = 0; i < opCount; i++) {
stack.dequeue();
}
long end = System.nanoTime();
return (end - start)/1000000000.0;
}
}
结果
ArrayeQueue:10.2110131
LinkedListQueue:0.0064676