0

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 + ", ");
    });
}
Pasha
  • 1,768
  • 6
  • 22
  • 43
  • Why do you assume this is wrong ? Could be some caching or recycling of some data that increase the performance of the second generation. – AxelH Apr 03 '18 at 13:32
  • Maybe. My aim is to compare the speed of parallel and non-parallel streams on the same collection or array. But even if I create 2 distinct arrays numbers_1 and numbers_2 with the same values the time measurements are the same – Pasha Apr 03 '18 at 13:38
  • 1
    Possible duplicate: https://stackoverflow.com/questions/504103/how-do-i-write-a-correct-micro-benchmark-in-java – lexicore Apr 03 '18 at 13:42
  • No, it’s not duplicate. – Pasha Apr 03 '18 at 13:48
  • You could replace your first array initialization loop by `int[] numbers = IntStream.range(0, 1000) .toArray();` That way, your code not only becomes shorter, you have also already loaded some of the stream classes before measuring (though not all). Still, [“How do I write a correct micro-benchmark in Java?”](https://stackoverflow.com/q/504103/2711488) *is* the Q&A you should read. It does not only answer your question, it also explains the other things you’re doing wrong and have not asked about (yet)… – Holger Apr 03 '18 at 15:54

1 Answers1

1

This question was already asked in a slightly different manner. Please have a look at this post:

Java8 stream operations are cached?

The first run takes longer as all classes and dependencies have to be loaded for the first time. If you'd extend your test up to, let's say, 10 runs you should get an almost identical result for the runs 2 to 10.