0    课程地址

https://coding.imooc.com/lesson/207.html#mid=13425

 

1    重点关注

1.1    数组队列和循环队列的比较

循环队列出队的复杂度循环队列为O(1),数组队列为O(n)

 

1.2    科学的测试方法

n次测试,求平均值

 

 

2    课程内容


 

3    Coding

3.1    数组队列和循环队列 复杂度 代码对比:

  • 测试类:
 /**
     * 主测试方法,测试循环队列和数组队列性能
     * @author weidoudou
     * @date 2022/10/27 7:24
     * @param queue 请添加参数描述
     * @param  count 请添加参数描述
     * @return long
     **/
    public static double getSecondComparLoopAndArray(Queue<Integer> queue,int count){
        long startTime = System.nanoTime();

        //入队
        for(int i = 0;i<count;i++){
            queue.enQueue(i);
        }

        //出队
        for(int i = 0;i<count;i++){
            queue.deQueue();
        }


        long endTime = System.nanoTime();
        double resultTime = (endTime-startTime)/1000000000.0;
        return resultTime;
    }

  //20w数据运行比较
    public static void main(String[] args) {

        int count = 200000;
        //数组队列
        QueueFirst<Integer> arrayQueue = new QueueFirst<>();
        //循环队列
        LoopQueue<Integer> loopQueue = new LoopQueue<>();

        double time1 = getSecondComparLoopAndArray(arrayQueue,count);
        System.out.println("time1==="+time1);
        double time2 = getSecondComparLoopAndArray(loopQueue,count);
        System.out.println("time2==="+time2);

    }

 

  • 测试结果:
time1===7.0273594
time2===0.0139386

Process finished with exit code 0

 

posted on 2022-10-27 07:18  菜鸟乙  阅读(33)  评论(0编辑  收藏  举报

目录导航