3

I was attempting to demonstrate to a Java novice streams and such. He said he had read that streams can be slower than for loops for primitive types. So, I set immediately to prove that wrong! What I got was a shock!

The for-loop and the serial stream run roughly the same, but the parallel stream is consistently and dramatically slower. Can someone explain why?

   @Test
    public void foo() throws Exception {

        Random r = new Random();

        IntSummaryStatistics stats = new IntSummaryStatistics();

        long start = System.currentTimeMillis();
        for(int i = 0; i < 100000000;i++) {
            stats.accept(r.nextInt() * 2);
        }
        System.out.println(System.currentTimeMillis() - start);

        start = System.currentTimeMillis();
        stats = r.ints(100000000).map(rn -> rn * 2).summaryStatistics();
        System.out.println(System.currentTimeMillis() - start);

        start = System.currentTimeMillis();
        stats = r.ints(100000000).parallel().map(rn -> rn * 2).summaryStatistics();
        System.out.println(System.currentTimeMillis() - start);


    }

resutls:

1067
1047
15184
Christian Bongiorno
  • 5,150
  • 3
  • 38
  • 76
  • 1
    Sometimes, sequential work will take less time than parallel work because of the resources needed to work in parallel. – Yassin Hajaj May 31 '17 at 20:57
  • there is a clearly example which a parallel stream is faster than `for-each` loop is when the task in the loop body has a very time-consuming task. e.g: accessing internet. – holi-java May 31 '17 at 21:09
  • 2
    I'd suggest reading Doug Lea's [When to use parallel streams](http://gee.cs.oswego.edu/dl/html/StreamParallelGuidance.html) – Stefan Zobel May 31 '17 at 21:20
  • 8
    Oh, and btw. I don't think that this question is a duplicate of [this one](https://stackoverflow.com/questions/23170832/java-8s-streams-why-parallel-stream-is-slower). Your problem is simply that you've used the **synchronized** `java.util.Random` as the producer of the IntStream. Choose `j.u.c.ThreadLocalRandom` instead and see what happens ... – Stefan Zobel May 31 '17 at 21:53
  • 2
    @StefanZobel I tried it and indeed increased the performance (2x slower -> 4x faster). Parallelism continues to amaze, thanks for the insight. Maybe Christian can re-open the question and answer it himself. – Malte Hartwig Jun 01 '17 at 09:54
  • 1
    @Stefan Zobel: or `SplittableRandom`, which might be even better (not only in terms of performance, but random distribution over multiple threads)… The `ThreadLocalRandom` might perform better than `Random` even in the sequential case (as its name suggests)… – Holger Jun 01 '17 at 09:55
  • @MalteHartwig the question is unlikely to get reopen since reviewers would most likely not read the comment section, and vote to keep it closed. I wouldn't be surprised if there was another appropriate dupe though, but I could not find one easily. Otherwise I think there are chatrooms for reopen reviews and changing dupe links. – Didier L Jun 01 '17 at 10:07
  • You could make your comments be answers then I can return – Christian Bongiorno Jun 01 '17 at 14:22
  • [This question](https://stackoverflow.com/questions/41380318/why-is-parallel-stream-slower) looks more like a true duplicate. @Christian Bongiorno How would you answer a question marked as duplicate :-) – Stefan Zobel Jun 01 '17 at 15:00

0 Answers0