Christoph Knabe, 2016-04-15
Massive parallel programming is possible with actors. The first edition of "Programming in Scala" says in chapter 30.4:
Unfortunately, despite their light-sounding name, threads are not all that cheap in Java. Threads use enough memory that typical Java virtual machines can have millions of objects but only thousands of threads. Worse, switching threads often takes hundreds if not thousands of processor cycles. If you want your program be as efficient as possible, then it is important to be sparing with thread creation and thread switching.
This benchmark example builds a chain of number parallel
- Threads (in Java), which are communicating by synchronized buffers, or
- Actors (in Scala with Akka), which are communicating by sending messages, respectively.
Then it sends number messages through the chain, resulting in square of number switchings between the threads respectively actors.
The measured times in milliseconds are:
number | Threads [ms] | Actors [ms] |
---|---|---|
10 | 9 | 34 |
100 | 85 | 220 |
1,000 | 5,390 | 2,470 |
10,000 | 529,856 | 37,988 |
Measured on a AMD Quad-Core processor FX7500 with up to 3.3 GHz in Eclipse at 2016-04-15.
For benchmarking threads on your computer run ThreadBufferChainMain.java. It runs the Thread benchmark with chains of lengths 10, 100, 1000, and 10000. Go away from your computer for about 600 seconds.
For benchmarking actors you have to run ActorMessageChainMain.scala. You will be prompted for the number of actors to be chained. Repeat this for 10, 100, 1000, and 10000.