Commonly used in Java 7 traversal speed comparison

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/sinat_22797429/article/details/86663979

Foreword

    During this contact time to return a list of sorted background problems, the list is returned to the end speed is very slow, the use of parallel streams Java8 fast manner, serial very slow, want to test different amounts of data Java8 Seven traversal speed Compare.
    Before using the method written inside the main method to compare, code review mechanisms architects say JVM run, the result is not a reference value, now by way of interface calls for comparison, as follows:

Interface Format

Speed ​​interface testing, where num capacity for incoming data, we were passing capacity of 10,000 and 30,000 two data to test the 7 ways to traverse

		 @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);
		 }

Traversal

1. Ordinary for loop

		//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 cycle

		//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. iterator loop

		//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));

result

The amount of data as the result of 10,000 and 30,000, and the results I had wanted a completely different, previously always thought that a parallel flow much faster than the serial stream, but the fact is ...

10000 amount of data:

Sort by Consuming (ms)
Ordinary for loop 48
forEach loop 46
Iterator loop 47
List.forEach 47
lists.stream().forEach 49
lists.parallelStream().forEach 61
lists.parallelStream().forEachOrdered 58

Processed Comparison:
forEach cyclic < List.forEach < iterator loop < Common for loop < lists.stream () forEach. < Lists.parallelStream () forEachOrdered. < Lists.parallelStream () forEach.

30,000 amount of data:

Sort by Consuming (ms)
Ordinary for loop 135
forEach loop 140
Iterator loop 140
List.forEach 137
lists.stream().forEach 138
lists.parallelStream().forEach 221
lists.parallelStream().forEachOrdered 144

Processed Comparison:
List.forEach < . Lists.stream () forEach < iterator loop <= forEach loop < Common for loop < lists.parallelStream () forEachOrdered. < Lists.parallelStream () forEach.

Guess you like

Origin blog.csdn.net/sinat_22797429/article/details/86663979