由于之前我们发现出队的时间复杂度不同,我们这里简单测试一下:
package com.company.queue;
import com.sun.javafx.binding.StringFormatter;
import java.util.Random;
/**
* @program: Array
* @description: 测试数组队列和循环队列
* @author: Gu
* @create: 2019-04-14 22:41
**/
public class TestQueue {
public double testQueue(Queue queue, int size) {
long start = System.nanoTime();
System.out.println(String.format("%s,开始时间 : %s \n",queue.getClass(), start));
Random random = new Random();
for (int i = 0 ; i < size; i++) {
queue.enqueue(random.nextInt(99999999));
}
for (int i = 0 ; i < size; i++) {
queue.dequeue();
}
long end = System.nanoTime();
System.out.println(String.format("%s,结束时间 : %s \n",queue.getClass(), end));
System.out.println(String.format("%s, 执行时间: %s \n",queue.getClass(), end - start));
System.out.println(String.format("----------------------------------------------\n"));
return (end - start) / 1000000000.0;
}
public static void main(String[] args) {
int size = 100000;
TestQueue testQueue = new TestQueue();
double arrayQueueTime = testQueue.testQueue(new ArrayQueue(size), size);
double loopQueueTime = testQueue.testQueue(new LoopQueue(size), size);
System.out.println(String.format("arrayQueueTime - loopQueueTime = %s",
arrayQueueTime - loopQueueTime));
}
}