Cool ideas revolving around computers and programming

Main menu

Category Archives: benchmark

Change of Heart

Mon Aug 17 17:51:58 CEST 2009
PLEASE NOTE:
I have had more time to study erlang since writing the article below and
have come to realize that the conclusions I drew are skewed.
Erlang
- is a language and a high-quality runtime system optimized for
concurrent and distributed execution. The examples below do not
really exercise any of these strengths.
- has proven itself in quite a number of high-profile projects and
deployments (ejabberd, CouchDb and RabbitMQ just to mention a few).
- has a strong, vibrant and competent community that's driving the
erlang system and improving it continuously.
For all these reasons stackless python -- although being an interesting
piece of technology in its own right -- is certainly no match for
erlang.
Please see http://pseudogreen.org/blog/erlang_vs_stackless_vs_multitask.html
for what appears to be a more even-handed comparison.

Introduction

I obtained a copy of Joe Armstrong’s Erlang book recently. After reading through the first half I have found it to be a very commendable work, making a strong case for message passing concurrency — a different approach to building large scale and highly concurrent systems.

After finishing chapter 8, I came across the following exercise:

Write a ring benchmark. Create N processes in a ring. Send a message round the ring M times so that a total of N*M messages get sent. Time how long this takes [..]

Write a similar program in some other programming language you are familiar with. Compare the results. Write a blog, and publish the results on the internet!

I liked it, it looked like a great opportunity to get my feet wet with Erlang and message passing concurrency 🙂

So, here we are, that’s precisely what this article is about.

The “other programming language” is of course Python, in this particular case Stackless Python featuring tasklets and channels to counter Erlang’s concurrency machinery.

The source code

Both the Erlang and the Stackless python code set up a ring of N processes so that each of the latter knows its successor. The resulting “ring” has the shape of an open necklace:

The loop() function is executed by the first N-1 processes in the ring. These know their position in the ring (S) as well as their immediate neighbour (NextP) to which they forward all messages received.

The messages (R) are just integers. Values above zero keep the ring going, a zero value (the last message) results in the termination of the ring.

I have made an attempt to keep the python code as similar as possible to its Erlang counterpart. The run_benchmark() function below builds the ring of N tasklets (lines 9-18) and sends the M messages around it (lines 19-21).

One minor difference to note: the main process/tasklet here is not the last process in the ring but sends the messages (after spawning the last process on lines 16-18).

Line 4-7 show the first message going around, lines 8-11 stem from the second message and lines 12-15 make the last (terminating) message visible (I guess the funny “order of execution” above arises from the fact that the Stackless python scheduler by default prefers the message receiver tasklets).

The first benchmarks

Now for the actual benchmarks, all run on a machine with the following specs: