Erlang: off and running

April, 2010

I’ve been reading through Armstrong’s book about Erlang as I mentioned last week, and decided to actually get my hands dirty and write some code. One the suggested exercises in the book is to write a ring benchmark program where you create a ring of N process and pass a message around that ring M times. The program should accept different values of N and M to vary both the number of processes in the ring and the number of times the message is passed around the ring. I decided I was going to do this both in Erlang and then in C# (which I’m much more experienced in) and compare both the code and some performance metrics.

I started writing the erlang program first because I wanted to try and solve the problem from an Erlang perspective and I though it would be interesting to try and think in a new language. Now, no laughing at my code, this is literally the first Erlang program I’ve ever written and I’m sure there are ways to improve this. Here is what the code looks like:

About 39 lines of code including white space and a couple of commented out lines. Next, I attempted to solve this same problem in C# and here is where I need to give some disclaimers and caveats. I knew going into this that there was no way that directly creating threads in C# was going to compete with the lightweight erlang processes. However, creating a solution that overcame this hurdle was a bit outside what I was trying to accomplish here so my end implementation in C# actually creates a thread for each node and uses and AutoResetEvent to provide a locking mechanism that allows the very basic passing of a message from one node to the next. I am sure there are issues with this, but here’s what I came up with. Actually the program is so much larger that it is hard to capture in a screen shot so here is a link to the gist if you want to see it in a better format. All in all it is about 140 lines including whitespace and my default C# formatting that I have setup with my Reshaper profile.

Now for some metrics! I’ll just present the basics here and you can run things on your own if you are curious. Just for the record, I took all these measurements on my Dell desktop (Windows 7 64bit, Xeon dual core 2.0 GHz, 8 GB RAM) although I did actually develop the erlang program on my mac (which was even faster than what I’m showing below).

Just a few words of explanation here. For the Erlang program, I wasn’t even able to measure a time in milliseconds until I got above 500 nodes in the process ring which is why there are no values there. The times for 10-100 nodes are in the microsecond range and aren’t worth showing on these graphs. For the C# program I started get OutOfMemory Exceptions when I got above 1000 nodes which is obviously due to the overhead of having 1 thread / node. Because of this there is no data beyond 1000 nodes. You can see that there is a massive difference between these two implementations even at the levels where the data does cross over. Creating 1000 processes in Erlang and passing a message around all 1000 processes 10 times took about 16 ms. Doing the same thing with threads in C# took almost 25 seconds.

If I were to spend more time with the C# program I could move to using the thread pool instead of discrete threads, and doing a better job of simulating message passing. However, the point is that Erlang has these ideas of concurrency built into the language constructs whereas in C# I’d have to create my own infrastructure to support similar features.

My last word on all this is about the difference between writing programs in the two languages. I uses Visual Studio and C# day in and day out, so writing that program wasn’t particularly difficult (plus I did it after solving the problem already once in Erlang). I do feel like programming in C# is often like writing a novel. The tools and intellisense are nice, but there is SO much typing that you have to do to get things done. Lots of files, lots of classes, many lines of codes. Even with intellisense, I often feel like my thoughts race ahead of what my fingers (and especially visual studio) are capable of doing. As much as I like VS, I do wait on it a lot.

I wrote my Erlang program in Emacs, which is a completely new editor for me (and fun to learn new key bindings after knowing vi, textmate and VS bindings) and it felt more like writing a piece of poetry than a novel. I though hard about the problem and the code I was writing, but did very little typing. Part of my slowness is obviously a lack of experience with the language, but in the end I felt like the language itself led me to solving the problem in a very koan like way.

Tim's Avatar Building GitHub since 2011, programming language connoisseur, San Francisco resident, aspiring surfer, father of two, life partner to @ktkates—all words by me, Tim Clem.