I've decided to compare the speed of processing parallel and non-parallel streams but to verify the correctness of the test I've made it on 2 parallel streams and the results seem to be wrong: the first stream takes about 60000000 nanoseconds while the second only 25000000.
Could you please explain me how should I fix the measurements? I provide a compiled method below so the problem is not a question of compiler optimization.
static void streamSpeed() {
int[] numbers = new int[1000];
for(int i = 0; i < 1000; numbers[i] = i++) {
;
}
long gap_2 = 0L;
long start = System.nanoTime();
List<Double> doubles_1 = (List)Arrays.stream(numbers).parallel().peek((ix) -> {
System.out.print(ix + ", ");
}).mapToDouble((ix) -> {
return (double)ix;
}).boxed().collect(Collectors.toList());
long gap_1 = System.nanoTime() - start;
System.out.println();
start = System.nanoTime();
List<Double> doubles_2 = (List)Arrays.stream(numbers).parallel().peek((ix) -> {
System.out.print(ix + ", ");
}).mapToDouble((ix) -> {
return (double)ix;
}).boxed().collect(Collectors.toList());
gap_2 = System.nanoTime() - start;
System.out.println();
System.out.println("Gap_1 : " + gap_1);
System.out.println("Gap_2 : " + gap_2);
doubles_1.forEach((ix) -> {
System.out.print(ix + ", ");
});
}