Further to this conversation on the Akka mailing list, I decided to benchmark various different approaches to implementing the producer/consumer pattern.
I wanted to choose a “real” problem, so I decided to count the words on the first 100,000 pages of Wikipedia. The producer parses the Wiki XML dump and the words are counted page-by-page by a pool of consumers.
I implemented three different approaches – producer pushes to a bounded queue, producer pushes to an unbounded queue together with a flow control protocol, and consumer pulls.
The source code for the different implementations is here, and the results are at the bottom of this message.
- I only timed to the nearest second as I see an approx. 3 second variation from run to run with identical parameters. I’m not sure why I see such a large variation—suggestions welcome.
- There’s basically no difference between the two “producer pushes” implementations. The “consumer pulls” implementation is much slower, however
- I tried both
SmallestMailboxRouterin the producer pushes implementations—the differences were too small to measure
I’m surprised that there is so much difference between producer pushes and consumer pulls. It’s quite possible that I’ve done something stupid in the implementation—I’d be very grateful for a pointer to what it is.
Here are the results (all on my i7 MacBook Pro—4 cores, 2 hyperthreads per core).