Java常用的7中遍历方式速度比较

前言

    这段时间接触到一个后台返回列表排序的问题,列表返回给前端速度很慢,采用Java8中并行流的方式很快,串行很慢,想测试一下Java8中不同数据量7种遍历方式速度的比较。
    之前采用main方法里面写的方法进行比较,代码走查架构师说JVM运行机制问题,结果不具有参考价值,现在采用接口调用的方式来进行比较,详情如下:

接口格式

测试速度的接口,其中num为传入的数据的容量,我们分别传入10000和30000两种的数据量,来测试一下7种方式遍历

		 @GetMapping("/speed")
		 public void testSpeed(@RequestParam Integer num){
		     List<Integer> lists = new ArrayList<>();
		     for (int i=0;i<num;i++){
		         lists.add(i);
		     }
		     testSpeed(lists);
		 }

遍历方式

1.普通for循环

		//1.普通for循环
        long before = System.currentTimeMillis();
        for (int i=0;i<lists.size();i++){
            System.out.print(i);
        }
        long after = System.currentTimeMillis();
        System.out.println();
        System.out.println("普通for循环:"+(after-before));

2.forEach循环

		//2.forEach循环
        long before2 = System.currentTimeMillis();
        for (int current:lists){
            System.out.print(current);
        }
        long after2 = System.currentTimeMillis();
        System.out.println();
        System.out.println("forEach循环:"+(after2-before2));

3.迭代器循环

		//3.iterator循环
        long before3 = System.currentTimeMillis();
        Iterator<Integer> iterator = lists.iterator();
        while (iterator.hasNext()){
            System.out.print(iterator.next());
        }
        long after3 = System.currentTimeMillis();
        System.out.println();
        System.out.println("迭代器循环:"+(after3-before3));

4.List.forEach

		//4.List.forEach循环
        long before4 = System.currentTimeMillis();
        lists.forEach(
                i -> System.out.print(i)
        );
        long after4 = System.currentTimeMillis();
        System.out.println();
        System.out.println("List.forEach:"+(after4-before4));

5.lists.stream().forEach

		//5.stream().forEach
        long before5 = System.currentTimeMillis();
        lists.stream().forEach(
                i-> System.out.print(i)
        );
        long after5 = System.currentTimeMillis();
        System.out.println();
        System.out.println("lists.stream().forEach:"+(after5-before5));

6.lists.parallelStream().forEach

		//6.parallelStream().forEach
        long before6 = System.currentTimeMillis();
        lists.parallelStream().forEach(
                i-> System.out.print(i)
        );
        long after6 = System.currentTimeMillis();
        System.out.println();
        System.out.println("lists.parallelStream().forEach:"+(after6-before6));

7.lists.parallelStream().forEachOrdered

		//7.parallelStream().forEachOrdered
        long before7 = System.currentTimeMillis();
        lists.parallelStream().forEachOrdered(
                i-> System.out.print(i)
        );
        long after7 = System.currentTimeMillis();
        System.out.println();
        System.out.println("lists.parallelStream().forEachOrdered:"+(after7-before7));

结果

通过10000和30000的数据量结果如下,结果和我之前想的完全不一样,之前一直以为并行流要比串行流快很多,但是事实却是…

10000数据量:

排序方式 耗时(ms)
普通for循环 48
forEach循环 46
迭代器循环 47
List.forEach 47
lists.stream().forEach 49
lists.parallelStream().forEach 61
lists.parallelStream().forEachOrdered 58

耗时比较:
forEach循环 < List.forEach < 迭代器循环 < 普通for循环 < lists.stream().forEach < lists.parallelStream().forEachOrdered < lists.parallelStream().forEach

30000数据量:

排序方式 耗时(ms)
普通for循环 135
forEach循环 140
迭代器循环 140
List.forEach 137
lists.stream().forEach 138
lists.parallelStream().forEach 221
lists.parallelStream().forEachOrdered 144

耗时比较:
List.forEach < lists.stream().forEach < 迭代器循环 <= forEach循环 < 普通for循环 < lists.parallelStream().forEachOrdered < lists.parallelStream().forEach

你可能感兴趣的:(java学习笔记)