Erlang Ring Code

Erlang Ring Benchmark Problem:
In the Programming Erlang book by Joe Armstrong, we find this problem at the end of the Concurrent Programming chapter.

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. 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!

My contribution to the already growing list of Erlang Ring Benchmark problem. However this is just the code for the Erlang, a N ring setup and M messages moving around the ring in parallel.

1 thought on “Erlang Ring Code”

  1. Try this…

    -module(proces).
    -export([do_ring/1, loopx/0, loopm/3, do_test/0]).

    do_ring(N) -> A = spawn(proces,loopx,[]),
    A ! {create, A, N-1},
    A.

    loopm(Pid0,NextPid, N) -> receive
    0 ->
    %% _ = io:format(“~s ~b~n”,[“0 in node”, N]),
    %% _ = io:format(“~w ~n”, [now()]),
    loopm(Pid0, NextPid, N);
    die ->
    %% _ = io:format(“~s ~b~n”,[“die in node”, N]),
    NextPid ! die;
    M when NextPid == Pid0 ->
    %% _ = io:format(“~b ~s ~b~n”,[M,”in node!”, N]),
    NextPid ! M-1,
    loopm(Pid0, NextPid, N);
    M ->
    %% _ = io:format(“~b ~s ~b~n”,[M,”in node”, N]),
    NextPid ! M,
    loopm(Pid0, NextPid, N)
    end.

    loopx() -> receive
    {create, Pid0, 0} ->
    loopm(Pid0,Pid0, 0);
    {create, Pid0, N} ->
    A = spawn(proces, loopx, []),
    A ! {create, Pid0, N-1},
    loopm(Pid0,A,N)
    end.

    do_test() ->
    P = do_ring(5000),
    _ = io:format(“~w ~n”, [now()]),
    P ! 1000,
    _ = io:format(“~w ~n”, [now()]).

Leave a Reply

Your email address will not be published. Required fields are marked *