Java List循环一般写法与stream性能对比

java 版本:

Java List循环一般写法与stream性能对比_第1张图片

如果需要对一个List进行循环,stream出来以后,我们又多了一种实现方式。但stream方式性能如何,各种说法都有,总体结论是没有一个标准答案,受各种环境影响。耳听为虚眼见为实,我们还是要自己动手试一下,才能有一个直观的感受,先直接上代码。

        int size = 10;
        // 为了防止读一个list读性能影响,各自读一个list
        List list1 = new ArrayList<>(size);
        List list2 = new ArrayList<>(size);
        List list3 = new ArrayList<>(size);
        List list4 = new ArrayList<>(size);
        List list5 = new ArrayList<>(size);
        List list6 = new ArrayList<>(size);
        for(int i = 0; i < size; i++) {
            list1.add(i);
            list2.add(i);
            list3.add(i);
            list4.add(i);
            list5.add(i);
            list6.add(i);
        }

        // 用线程池来执行
        ExecutorService executorService = Executors.newFixedThreadPool(6);

        executorService.submit(()->{
            long t = System.nanoTime();
            List result = new ArrayList<>(size);
            for (int i = 0; i {
            long t = System.nanoTime();
            List result = new ArrayList<>(size);
            for(Integer i : list3){
                result.add(i.toString());
            }
            System.out.println("for i:list time:         " + (System.nanoTime()-t));
        });

        executorService.submit(()->{
            long t = System.nanoTime();
            List result = new ArrayList<>(size);
            list4.forEach(i->{
                result.add(i.toString());
            });
            System.out.println("forEach time:            " + (System.nanoTime()-t));
        });
        executorService.submit(()->{
            long t = System.nanoTime();
            List result = new ArrayList<>(size);
            list5.stream().forEach(i->{
                result.add(i.toString());
            });
            System.out.println("stream forEach time:     " + (System.nanoTime()-t));
        });
        executorService.submit(()->{
            long t = System.nanoTime();
            List result = new ArrayList<>(size);
            Iterator iterator = list6.iterator();
            while (iterator.hasNext()){
                result.add(iterator.next().toString());
            }
            System.out.println("Iterator time:           " + (System.nanoTime()-t));
        });

        executorService.submit(()->{
            long t = System.nanoTime();
            List result = new ArrayList<>(size);
            list1.stream().parallel().forEach(i->{
                result.add(i.toString());
            });
            System.out.println("stream parallel time:    " + (System.nanoTime()-t));
        });
        executorService.shutdown();


size=10时间

for i i++ for time:      53600
for i:list time:         44300
forEach time:            2022100
Iterator time:           45800
stream forEach time:     3911300


size=100时间

for i i++ for time:      184000
for i:list time:         130900
Iterator time:           125600
forEach time:            2322100
stream forEach time:     4455200
stream parallel time:    8065700

size=1000时间

for i i++ for time:      765600
for i:list time:         1880400
forEach time:            1679600
Iterator time:           379400
stream forEach time:     3801400
stream parallel time:    8549700


size=10000(1W)时间

for i i++ for time:      6572000
for i:list time:         7531800
forEach time:            7275200
Iterator time:           2865600
stream forEach time:     13723200
stream parallel time:    11157400

size=100000(10W)时间

for i i++ for time:      55485700
stream forEach time:     53776100
Iterator time:           54551800
for i:list time:         61173500
stream parallel time:    21724400
forEach time:            58461600


size=1000000(100W)时间

Iterator time:           2016258400
for i i++ for time:      2653177400
stream parallel time:    2022854000
stream forEach time:     2034483500
for i:list time:         2045225900
forEach time:            2051831300


基本结论:

从上面数据得出结论是数据量对性能评价影响较大,数据量无排序要求时推荐用stream parrellel,数据量比较小(100W以下),一般业务场景下,还是老老实实用普通写法吧,毕竟这么多年的优化,基本写法效率还是很高的。

根据Java List循环一般写法与stream性能对比_笑笑和哈哈的博客-CSDN博客重新测试

你可能感兴趣的:(Java基础,#,集合&Map,java)