[Java] [Java] Program 10x faster with parallelStream

1 minute read

Conclusion

Java programs can be 10x faster with parallelStream()

Environment

  • CPU: Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz, 2208 Mhz, 6 cores, 12 logical processors
  • Memory: 16GB
  • Java: Amazon Corretto-11.0.3.7.1

Verification contents

Verification code

Main.java


import java.util.List;
import java.util.ArrayList;

public class Main {
public static void main(String[] args) {
List<Integer> n1 = new ArrayList<>();
List<Integer> n2 = new ArrayList<>();

for(int i = 0; i <500; i++) {
n1.add(i);
n2.add(i);
}

long s1 = System.currentTimeMillis();
n1.stream().forEach(i -> {try {Thread.sleep(10);} catch(Exception e) {e.printStackTrace(); }});
long t1 = System.currentTimeMillis();

long s2 = System.currentTimeMillis();
n2.parallelStream().forEach(i -> {try {Thread.sleep(10);} catch(Exception e) {e.printStackTrace(); }});
long t2 = System.currentTimeMillis();

System.out.printf("n1 exec time: %d [ms]\n", t1-s1);
System.out.printf("n2 exec time: %d [ms]\n", t2-s2);
}
}

There aren’t many points to collect and explain. n1 is a sequential stream and n2 is a parallel stream, calling Thread.sleep(10) 500 times. The reason for try-catch is to handle the checked exception of Thread.sleep(). The i in forEach disappears into the void.

Execution result

n1 exec time: 5298 [ms]
n2 exec time: 505 [ms]

n2 is more than 10 times faster than n1! As for n1, the result of 10ms × 500 times really appears.

Supplement

According to official document, the default number of parallel threads is , Which is equal to the number of processors available on the computer.

For applications that require separate or custom pools, a ForkJoinPool may be constructed with a given target parallelism level; by default, equal to the number of available processors.

It seems that the number of logical processors is applied as the default value instead of the number of physical processors, as you can see that the execution time for parallel streams is 1/10 or less of that for sequential streams. “It’s 10 times faster”… Omakan !! I think that there is a very reasonable tsukumi, but for most modern PCs I think there are about 8 logical processors, so please forgive me.

Note

Basically, it is better not to update or delete the objects commonly referred to by each thread (such as add to List).