We would like to know how to benchmark parallel Stream.
/*from w w w .j a v a 2 s . c om*/ import java.util.stream.IntStream; public class Main { public static void main(String[] argv) { System.out.println("Benchmarking..."); long t0 = System.nanoTime(); int a[] = IntStream.range(0, 1_000_000).filter(p -> p % 2 == 0).toArray(); long t1 = System.nanoTime(); int b[] = IntStream.range(0, 1_000_000).parallel().filter(p -> p % 2 == 0) .toArray(); long t2 = System.nanoTime(); System.out.printf("serial: %.2fs, parallel %.2fs%n", (t1 - t0) * 1e-9, (t2 - t1) * 1e-9); } }
The code above generates the following result.