Home > Back-end >  Questions about parallelStream execution speed
Questions about parallelStream execution speed

Time:01-19

In this second set of data to test the
Have found that stream faster than parallelStream, not parallelStream should be faster?

This is where there is a problem,



List list_value=https://bbs.csdn.net/topics/new ArrayList <> ();

List Temp1=new ArrayList<> (a);

List Temp2=new ArrayList<> (a);


for(int i=0; I<40000000; I++)
{

List_value. Add (String. The valueOf (I));

}

LocalDateTime start=LocalDateTime. Now ();

//test:

Stream StringStream=list_value. Stream (). The filter (x & gt; (Integer. ParseInt (x) % 2)==0);

List Collect=list_value. ParallelStream (). The filter (x & gt; (Integer. ParseInt (x) % 2)==0). Collect (Collectors. ToList ());



//test 2:

List_value. Stream (). The filter (x - & gt; (Integer. ParseInt (x) % 2)==0). The forEach (x & gt; Temp1. Add (String. The valueOf (x)));


List_value. ParallelStream (). The filter (x - & gt; (Integer. ParseInt (x) % 2)==0). Collect (Collectors. ToList ()). The forEach (x & gt; Temp2. Add (String. The valueOf (x)));



LocalDateTime end=LocalDateTime. Now ();

Duration timeElapsed=Duration. Between (start, end);

System. Out. Println (temp1. The size () + "\ r \ n" + temp2. The size () + "\ r \ n + start" + "\ r \ n" + "\ r \ n" + + end timeElapsed);

CodePudding user response:

Test 1: the first statement
 Stream StringStream=list_value. Stream (). The filter (x & gt; (Integer. ParseInt (x) % 2)==0); 
no foreach, termination of operations such as collect, there would be no real implementation;

Test 2: the first statement, the results add to the temp1, directly to the second statement, Mr Became a temporary list, and then added to traverse the list are temp2; Consider whether because the data to add one more time and traversal, influenced the execution efficiency

CodePudding user response:

Test 1: the first statement, there is no foreach, collect and termination of the operation, there would be no real implementation;
However, such screening of the returned result is right, is indeed a % 2 results back to, stringStream inside



Test 2: the first statement, the results add to the temp1, directly to the second statement, Mr Became a temporary list, and then added to traverse the list are temp2; Consider whether because the data to add one more time and traversal, affects the execution efficiency

That, it seems that parallelStream execution efficiency is no Stream slow after ather
Because if you don't use. Collect (Collectors. ToList ()), is the likely outcome of a parallel out wrong, this step could not save
  • Related