数组队列和循环队列的区别

package com.practice;

import java.util.Random;

public class Main {

//测试使用queue运行opCount个enqueue和dequeue操作所需要的时间,单位:秒
private static double testQueue(Queue<Integer> queue, int opCount){

long startTime = System.nanoTime();

Random random = new Random();
for (int i = 0; i < opCount; i++) {
queue.enqueue(random.nextInt(Integer.MAX_VALUE));
}
for (int i = 0; i < opCount; i++) {
queue.dequeue();
}
long endTime = System.nanoTime();

return (endTime - startTime) / 1000000000.0;
}

public static void main(String[] args) {

int opCount = 100000;

ArrayQueue<Integer> arrayQueue = new ArrayQueue<>();
double time1 = testQueue(arrayQueue,opCount);
System.out.println("AyyayQueue , time :" + time1 + "s");

LoopQueue<Integer> loopQueue = new LoopQueue<>();
double time2 = testQueue(loopQueue,opCount);
System.out.println("LoopQueue , time :" + time2 + "s");
}
}

运行结果:

AyyayQueue , time :6.9565154s
LoopQueue , time :0.0176564s

posted on 2022-07-14 19:49  网恋被骗两千八  阅读(22)  评论(0编辑  收藏  举报