Following my post Which language to learn next I picked up the pdf book Programming Erlang and have devoted and hour or two a day for the last week to learning Erlang.
One of the early concurrent programming exercises goes like this:
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 for different values of N and M.
I won't post a solution because that kinda defeats the purpose of the exercise, but here is my start function with the timing code. For clarity, the call to initproc/4 causes initproc/4 to recurse N-1 times.
start(N, M) when N > 0 ->
MasterPid = spawn(fun() -> initproc(master) end),
FirstChildPid = spawn(fun() -> initproc(2, N, M, MasterPid) end),
register(ctl, self()),
statistics(runtime),
statistics(wall_clock),
MasterPid ! {start, M, FirstChildPid, hola},
receive
_Any ->
{_, Time1} = statistics(runtime),
{_, Time2} = statistics(wall_clock),
io:format("Run time: ~p (~p) ms~n", [Time1, Time2]),
unregister(ctl)
end.
Here are some results from my Macbook 2GHz, 2Gb ram:
10 Procs, 100 messages -> Run time: 0 (1) ms 10 Procs, 1000 messages -> Run time: 0 (5) ms
Messaging is obviously very fast in Erlang!
10 Procs, 10000 messages -> Run time: 30 (32) ms 100 Procs, 10000 messages -> Run time: 300 (308) ms
Can it be that linear?
1000 Procs, 10000 messages -> Run time: 2920 (3043) ms 1000 Procs, 100000 messages -> Run time: 28840 (29974) ms
Crikey!
10000 Procs, 100 messages -> Run time: 580 (614) ms 10000 Procs, 1000 messages -> Run time: 6060 (6279) ms 10000 Procs, 10000 messages -> Run time: 61420 (64036) ms
It took me a good half hour to become bored by playing with different values of M and N! (Should this be embarrassing or a badge of honour?)
Of course this is a toy application, but it's pretty obvious Erlang is a serious bit of kit.
Programming Erlang is a well written book from which I've already learned so much, and not just about Erlang, but programming in general. All up I'm excited to continue the journey.

Feed
Comments are now closed.