-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParallelStreamExample.java
More file actions
30 lines (21 loc) · 983 Bytes
/
ParallelStreamExample.java
File metadata and controls
30 lines (21 loc) · 983 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package streams.parallel_streams;
/*
* Example demonstrating the efficiency of parallel stream over normal stream for large data sets
*/
import java.util.stream.LongStream;
public class ParallelStreamExample {
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
long sumSequential = LongStream.rangeClosed(1, 10_000_000)
.sum();
long endTimeSequential = System.currentTimeMillis();
long sumParallel = LongStream.rangeClosed(1, 10_000_000)
.parallel()
.sum();
long endTimeParallel = System.currentTimeMillis();
System.out.println("Sum (Sequential): " + sumSequential);
System.out.println("Time taken (Sequential): " + (endTimeSequential - startTime) + "ms");
System.out.println("Sum (Parallel): " + sumParallel);
System.out.println("Time taken (Parallel): " + (endTimeParallel - endTimeSequential) + "ms");
}
}