-1

I made the following code.

    @Test
    public void parallelStream() {
        IntStream.rangeClosed(0,10_000).boxed().parallel()
                        .forEach(System.out::println);
    }

    @Test
    public void parallelStreamOrdered() {
        IntStream.rangeClosed(0,10_000).boxed().parallel()
                .forEachOrdered(System.out::println);
    }

As a result of the performance, parallelStream took 178 ms and parallelStreamOrder took 33 ms.

I know that in parallel, .forEach() is non-deterministic, and .forEachOrdered() is guaranteed order.

But is it related to speed? It was expected that keeping the order would slow down the speed. I wonder why the speed is so different.

ilovek
  • 3
  • 2
  • 1
    You are measuring the performance of an operation that doesn’t work in parallel at all, `System.out.println(…)` which is internally `synchronized`. The most efficient execution of this example would be single threaded, with all synchronizations merged. You are comparing the bad with the worse. – Holger Nov 15 '21 at 08:55

1 Answers1

-1

I think you should test this with some big value then the result would be much more clear and there will be a high time difference.

Umair
  • 585
  • 3
  • 9
  • 21