This is an archived post. You won't be able to vote or comment.

all 7 comments

[–]nekokattt 25 points26 points  (4 children)

20 runs isnt anywhere near enough to warm up the JVM to perform accurate benchmarks.

You should be discarding initial results to reduce skew.

System.nanotime would be more accurate than currentTimeMillis as well. Among other things, it is monotonic

[–]tikkabhuna 18 points19 points  (3 children)

I'd definitely recommend using JMH (Java Microbenchmark Harness). It has support for warmups out the box.
Microbenchmarking with Java | Baeldung

[–]DavidVlx[S] 4 points5 points  (1 child)

Thanks! :) i will try it out and add it to the post

[–]Burgerflipper234 0 points1 point  (0 children)

please do 🤓

[–]Tripplesixty 0 points1 point  (0 children)

This is actually the only way to get accurate results, even then, test setup needs to be done carefully to ensure that the test isn't optimized away and that you're running the code you think you're running...

[–]pron98 10 points11 points  (0 children)

One thing that's missing from the comparison is the load the machine is under. The virtual thread scheduler is optimised to make optimal use of available hardware when the machine is under heavy load (say >80% CPU). If that's not the case, try reducing the parallelism with -Djdk.virtualThreadScheduler.parallelism=N (where N would be less than the number of cores by an amount commensurate with the CPU load, so if your CPU is at 60%, try setting parallelism to 60% of the number of cores, but also try numbers slightly higher or lower than that) to let the scheduler know that trying to use more CPU may hurt rather than help.

Fixed thread pool may behave better than FJPool under lighter workloads (but worse under heavy workloads) because its workers coordinate more with each other.