Threads vs Coroutines - ParallelMap Performance

Published: 09 December 2022
on channel: Pairing with Duncan
1,534
46

In the last episode (   • Threads vs Coroutines - ParallelMap P...  ) we implemented a parallelMap operation using streams, raw threads, a threadpool with futures, and coroutines. At first glance the raw threads was quickest, followed by futures, coroutines and then streams.

In this, part 56 of an exploration of where a Test Driven Development implementation of the Gilded Rose stock control system might take us in Kotlin, we investigate the performance of the different functions further, in particular digging down into why coroutines seem to be slow and finding a way to speed them up. We also find a way to use a particular ForkJoinPool to run the streams code, making it as fast as the others (bar the raw threads).

Frankly we only use very rough benchmarks here, with no statistical testing except 'it looks like'. That's OK for gross differences, but is highly suspect when deciding which of two similarly performant approaches is faster. For that check out JMH https://github.com/openjdk/jmh
and you could watch my video from KotlinConf 2017 -    • KotlinConf 2017 - The Cost of Kotlin ...   . [The next episode also has a bit of a better treatment of the benchmarks -    • Threads vs Coroutines - Errors and Ca...  ]

You can see the whole of this series as a playlist
   • All Gilded Rose Episodes  
and the code on GitHub
https://github.com/dmcg/gilded-rose-tdd

If you like this, you’ll probably like the book Java to Kotlin, A Refactoring Guidebook
http://java-to-kotlin.dev
Also if you're going to KotlinConf in April 2023, you could work with Nat Pryce and me at our TDD workshop at KotlinConf https://kotlinconf.com/workshops/

The music in this episode is used with permission from http://toolofgod.com/my-music/royalty... -


Watch video Threads vs Coroutines - ParallelMap Performance online without registration, duration hours minute second in high quality. This video was added by user Pairing with Duncan 09 December 2022, don't forget to share it with your friends and acquaintances, it has been viewed on our site 1,534 once and liked it 46 people.