This is not a duplicate of How do I write a correct micro-benchmark in Java?
The question goes to the heart of Project Loom design and implementation, and whether the project has been able to speed up the performance of Java Parallel Streams or not. The question is not about bench-marking, it is about intent of Project Loom.
I am experimenting with Virtual Threads in my loom-lab project, and wanted to see if Virtual Threads are faster than Platform Threads when doing a parallel stream, but it actually seems slower.
try (var executorService = Executors.newThreadPerTaskExecutor(virtualThreadFactory)) {
var candidates3 = LongStream.iterate(3, x -> x < count, x -> x + 2);
time4 = System.currentTimeMillis();
var primes3 = executorService.submit(() ->
candidates3.parallel()
.filter(candidate -> isPrime(candidate)).toArray()
).get();
time5 = System.currentTimeMillis();
}
where ultimately I get the output (in milliseconds)
sequential time = 7546
parallel time = 1289
virtual time = 1388
and in general using Virtual Threads is slower than the common ForkJoinPool. Am I making some basic mistake or misunderstanding somewhere, or has Project Loom not been integrated with Java Streams yet?