Abstract: For today's puzzle, we are getting elements from Vector, sequentially and in parallel. But why is the performance so much worse in Java 15-ea+28?
Welcome to the 280th edition of The Java(tm) Specialists' Newsletter, sent to you from the Island of Crete. We spent the past three weeks doing live classes on topics like concurrency, design patterns and dynamic proxies. It was a lot of fun and the feedback from the students was amazing. Even though we repeated the content in the afternoons, some of the students liked the sessions so much that they came to both. Please let me know by email if you would like me to teach private live classes to your team of programmers.
javaspecialists.teachable.com: Please visit our new self-study course catalog to see how you can upskill your Java knowledge.
So I wrote this little class this afternoon:
import java.util.*; import java.util.stream.*; public class VectorBench { public static void main(String... args) { for (int i = 0; i < 10; i++) { test(false); test(true); } } private static void test(boolean parallel) { IntStream range = IntStream.range(1, 100_000_000); if (parallel) range = range.parallel(); long time = System.nanoTime(); try { ThreadLocal<List<Integer>> lists = ThreadLocal.withInitial(() -> { List<Integer> result = new Vector<>(); for (int i = 0; i < 1024; i++) result.add(i); return result; }); range.map(i -> lists.get().get(i & 1023)).sum(); } finally { time = System.nanoTime() - time; System.out.printf("%s %dms%n", parallel?"parallel":"sequential", (time / 1_000_000)); } } }
Here are the results for different versions of Java running on my 1-6-2 MacBook Pro Late 2018 model.
Java 8 and 9 have similar performance characteristics:
openjdk version "1.8.0_252" OpenJDK Runtime Environment (build 1.8.0_252-b14) OpenJDK 64-Bit Server VM (build 25.252-b14, mixed mode) sequential 1576ms parallel 233ms sequential 1560ms parallel 185ms sequential 1538ms parallel 101ms sequential 538ms parallel 103ms sequential 560ms parallel 103ms sequential 549ms parallel 102ms sequential 554ms parallel 103ms sequential 546ms parallel 103ms sequential 556ms parallel 103ms sequential 548ms parallel 104ms openjdk version "9.0.7.1" OpenJDK Runtime Environment OpenJDK 64-Bit Server VM sequential 1618ms parallel 220ms sequential 1512ms parallel 192ms sequential 1516ms parallel 103ms sequential 557ms parallel 105ms sequential 556ms parallel 106ms sequential 559ms parallel 111ms sequential 587ms parallel 108ms sequential 554ms parallel 104ms sequential 564ms parallel 105ms sequential 599ms parallel 131ms
In Java 10, the first few results are faster, but later results are roughly the same:
openjdk version "10.0.2" 2018-07-17 OpenJDK Runtime Environment OpenJDK 64-Bit Server VM sequential 717ms parallel 148ms sequential 591ms parallel 107ms sequential 581ms parallel 106ms sequential 592ms parallel 106ms sequential 561ms parallel 107ms sequential 555ms parallel 109ms sequential 572ms parallel 108ms sequential 566ms parallel 110ms sequential 558ms parallel 105ms sequential 568ms parallel 121ms openjdk version "11.0.7" 2020-04-14 LTS OpenJDK Runtime Environment OpenJDK 64-Bit Server VM sequential 612ms parallel 154ms sequential 560ms parallel 118ms sequential 599ms parallel 110ms sequential 567ms parallel 109ms sequential 562ms parallel 122ms sequential 578ms parallel 109ms sequential 571ms parallel 109ms sequential 569ms parallel 109ms sequential 572ms parallel 113ms sequential 565ms parallel 120ms openjdk version "12.0.2" 2019-07-16 OpenJDK Runtime Environment OpenJDK 64-Bit Server VM sequential 597ms parallel 141ms sequential 526ms parallel 103ms sequential 518ms parallel 104ms sequential 549ms parallel 105ms sequential 522ms parallel 107ms sequential 572ms parallel 107ms sequential 531ms parallel 108ms sequential 534ms parallel 104ms sequential 531ms parallel 107ms sequential 536ms parallel 120ms openjdk version "13.0.3" 2020-04-14 OpenJDK Runtime Environment OpenJDK 64-Bit Server VM sequential 606ms parallel 140ms sequential 538ms parallel 106ms sequential 540ms parallel 108ms sequential 528ms parallel 113ms sequential 536ms parallel 108ms sequential 533ms parallel 107ms sequential 530ms parallel 108ms sequential 534ms parallel 107ms sequential 545ms parallel 116ms sequential 553ms parallel 120ms openjdk version "14.0.1" 2020-04-14 OpenJDK Runtime Environment (build 14.0.1+7) OpenJDK 64-Bit Server VM (build 14.0.1+7, mixed mode, sharing) sequential 705ms parallel 136ms sequential 579ms parallel 112ms sequential 572ms parallel 113ms sequential 583ms parallel 112ms sequential 579ms parallel 111ms sequential 566ms parallel 111ms sequential 571ms parallel 109ms sequential 591ms parallel 108ms sequential 572ms parallel 110ms sequential 573ms parallel 120ms
Things improved a bit in Java 15 (last month's JDK).
openjdk version "15-ea" 2020-09-15 OpenJDK Runtime Environment (build 15-ea+23-1098) OpenJDK 64-Bit Server VM (build 15-ea+23-1098, mixed mode, sharing) sequential 709ms parallel 133ms sequential 547ms parallel 98ms sequential 551ms parallel 97ms sequential 537ms parallel 97ms sequential 545ms parallel 100ms sequential 540ms parallel 98ms sequential 540ms parallel 99ms sequential 543ms parallel 98ms sequential 552ms parallel 109ms sequential 586ms parallel 107ms
Or maybe not?
openjdk version "15-ea" 2020-09-15 OpenJDK Runtime Environment (build 15-ea+28-1400) OpenJDK 64-Bit Server VM (build 15-ea+28-1400, mixed mode, sharing) sequential 1508ms parallel 236ms sequential 1553ms parallel 199ms sequential 1517ms parallel 201ms sequential 1449ms parallel 211ms sequential 1568ms parallel 205ms sequential 1478ms parallel 199ms sequential 1540ms parallel 205ms sequential 1488ms parallel 204ms sequential 1499ms parallel 203ms sequential 1523ms parallel 202ms
What is going on? Please let me know your guesses by return email. Best explanation gets a mention in the next newsletter :-)
Kind regards from Crete
Heinz
We are always happy to receive comments from our readers. Feel free to send me a comment via email or discuss the newsletter in our JavaSpecialists Slack Channel (Get an invite here)
We deliver relevant courses, by top Java developers to produce more resourceful and efficient programmers within their organisations.