-3

I was learning java stream api and I found this issue in code.

Here is my code

 long s = System.currentTimeMillis();
 IntStream.range(1, 10).parallel().forEach( x ->{
      System.out.println( "Thread -> "+ Thread.currentThread().getName() +" "+ x );
 });
 long e = System.currentTimeMillis();
 System.out.println("Time taken "+ (e-s) + " ms");

 System.out.println("---------------------");

 s = System.currentTimeMillis();
 IntStream.range(1, 10).forEach( x ->{
     System.out.println( "Thread -> "+ Thread.currentThread().getName() +" "+ x );
 });
 e = System.currentTimeMillis();
 System.out.println("Time taken "+ (e-s) + " ms");

Here is the output

Thread -> main 6
Thread -> ForkJoinPool.commonPool-worker-7 2
Thread -> ForkJoinPool.commonPool-worker-7 5
Thread -> ForkJoinPool.commonPool-worker-13 1
Thread -> ForkJoinPool.commonPool-worker-11 4
Thread -> ForkJoinPool.commonPool-worker-3 3
Thread -> ForkJoinPool.commonPool-worker-15 9
Thread -> ForkJoinPool.commonPool-worker-9 7
Thread -> ForkJoinPool.commonPool-worker-5 8
Time taken using parallel stream 29 ms
-------------------------------------
Thread -> main 1
Thread -> main 2
Thread -> main 3
Thread -> main 4
Thread -> main 5
Thread -> main 6
Thread -> main 7
Thread -> main 8
Thread -> main 9
Time taken simple stream 1 ms

I have tried this code on sts and inteliJ IDE, results are same. Parallel is taking longer time than sequential. Is there a problem with my jdk? please suggest.

  • 1
    What exactly are you trying to do in parallel? It looks like exactly nothing. So you’re paying the **massive** cost of parallelism and then doing nothing with it - exactly printing to the console ... which is synchronised. – Boris the Spider May 14 '21 at 06:21
  • Perhaps more importantly, this is most definitely not how you microbench Java. In fact the system clock should not be used for this sort of measurement in _any_ language. – Boris the Spider May 14 '21 at 06:23
  • 2
    Second duplicate: [Java 8's streams: why parallel stream is slower?](https://stackoverflow.com/questions/23170832/java-8s-streams-why-parallel-stream-is-slower) – luk2302 May 14 '21 at 06:24
  • 1
    Really this is two problems - your benchmark is wrong, and your use of parallelism is wrong. The duplicate addresses the first problem. – Boris the Spider May 14 '21 at 06:24
  • @BoristheSpider one of the user did the same thing and there was huge difference between execution time. Parallel execution time was almost half of sequential execution. But if I use sequential first and then parallel in the code , then execution time is for sequential is double of parallel – Sheshanath Kumar May 14 '21 at 06:31
  • Did you not read any of the linked posts? – luk2302 May 14 '21 at 06:31
  • @luk2302 .. sorry going thorough it.. – Sheshanath Kumar May 14 '21 at 06:33
  • 1
    “_One other user_” - reference? If the timing changes with order then what you’re likely timing is classloading. I very much doubt the code runs in anything other than interpreted mode given the tiny scale - so unlikely JIT is getting involved. See links about writing useful microbechmarks. – Boris the Spider May 14 '21 at 06:35

1 Answers1

2

First of all, doing this kind of benchmarks is hard. All kinds of optimizations are performed during execution of the code. But in this case, I wouldn't expect a much different result when the benchmark would have been good.

First, parallellisation costs overhead. You need to start and stop threads, sometimes synchronize them at certain points, communicate between them, etc. So usually it is only worth the effort if there are a lot a elements to process, and the work which needs to be done for each element is non-trivial. Both are not true in your case.

Secondly, you are outputting to standard out. System.out will only allow one thread to output something at the same time. This is done, so that output isn't scrambled too much if multiple threads try to write to standard out. This means that in your case, all threads will basically spent a lot of time waiting for their turn to write.

So, no, there is nothing wrong with your JVM.

Hoopje
  • 12,677
  • 8
  • 34
  • 50