## January 3, 2011

### Erlang Ring Benchmark Part 3

In Ring Benchmark Part 2 I presented a more compact solution to the ring benchmark. It's performance is about same as the original solution that was using pure recursion to setup the ring. Both part 1 and part 2 were waiting for a message to go around the ring completely, before injecting the next message. In this version we will inject messages 1 through M as soon as the ring is setup, this considerably improves throughput. Line 10 below is the part where we spawn off a process to inject M..0 messages into the ring.

### Timings Across Part 1-3

• Part 1: N=1000, M=1000, Run Time = 600 ms
• Part 2: N=1000, M=1000, Run Time = 600 ms
• Part 3: N=1000, M=1000, Run Time = 400 ms

### Code

```-module(ring3_2).
-include("debug.hrl").
-export([start/2]).
%% ring benchmark using lists methods
start(N, M) ->
io:format("Starting ring benchmark on pid=~p, N = ~p, M = ~p.~n", [self(), N, M]),
statistics(wall_clock),
statistics(runtime),
Last = lists:foldl(
fun (X, Pid) ->
spawn(fun () -> loop(X, Pid) end) end, self(), lists:seq(N-1, 1, -1)),
spawn(fun () ->
lists:foreach(fun (X) -> Last ! X end, lists:seq(M-1, 0, -1)) end),
wait(),
{_, WC} = statistics(wall_clock),
{_, RT} = statistics(runtime),
io:format("Done running ring benchmark in Wall Clock = ~p, Runtime = ~p.~n", [WC, RT]).

wait() ->
Xm when Xm =:= 0 ->
?DEBUG("main node done waiting for x = ~p.~n", [Xm]),
void;
Xm -> ?DEBUG("main node done waiting for x = ~p.~n", [Xm]), wait()
end.

loop(Nth, Pid) ->