<div dir="ltr">Hello there,<div><br></div><div>I need help with code review on my attempt to following problem</div><div><br></div><div><span style="color:rgb(0,0,0);font-family:verdana,sans-serif;font-size:18px;line-height:23.3999996185303px"> Write a ring benchmark. Create </span><code style="font-size:14px;color:rgb(0,0,0);line-height:23.3999996185303px">N</code><span style="color:rgb(0,0,0);font-family:verdana,sans-serif;font-size:18px;line-height:23.3999996185303px"> processes in a ring. Send a message round the ring </span><code style="font-size:14px;color:rgb(0,0,0);line-height:23.3999996185303px">M</code><span style="color:rgb(0,0,0);font-family:verdana,sans-serif;font-size:18px;line-height:23.3999996185303px"> times so that a total of </span><code style="font-size:14px;color:rgb(0,0,0);line-height:23.3999996185303px">N * M </code><a name="of" style="color:rgb(233,131,0);font-family:verdana,sans-serif;font-size:18px;line-height:23.3999996185303px"></a><span style="color:rgb(0,0,0);font-family:verdana,sans-serif;font-size:18px;line-height:23.3999996185303px">messages get sent. Time how long this takes for different values of </span><code style="font-size:14px;color:rgb(0,0,0);line-height:23.3999996185303px">N</code><span style="color:rgb(0,0,0);font-family:verdana,sans-serif;font-size:18px;line-height:23.3999996185303px"> and </span><code style="font-size:14px;color:rgb(0,0,0);line-height:23.3999996185303px">M</code><span style="color:rgb(0,0,0);font-family:verdana,sans-serif;font-size:18px;line-height:23.3999996185303px">.</span><br></div><div><br></div><div>I have not timed this yet, and guidance on recommended ways to time is very much appreciated.</div><div><br></div><div>For now, following is the code for ring that I wrote. Please let me know if there are any shortcomings or code could be written in an much idiomatic way.</div><div><br></div><div>Thanks a lot</div><div>+ Harit</div><div><br></div><div><u><b>Code</b></u></div><div><div><br></div><div>-module(ring).</div><div>-author("harith").</div><div><br></div><div>%% API</div><div>-export([message/2]).</div><div><br></div><div>% create ring of N processes and</div><div>% send M messages between them</div><div>message(N, M) when is_integer(N), is_integer(M), N > 0, M > 0 -></div><div>  Ring = create_ring(N),</div><div>  [Start | T] = Ring,</div><div>  Start ! {T, Ring, 1, M}.</div><div><br></div><div>create_ring(N) -></div><div>  Processes = [spawn(fun() -> loop() end) || _ <- lists:seq(1, N)],</div><div>  [H | _] = Processes,</div><div>  lists:append(Processes, [H]).</div><div><br></div><div>loop() -></div><div>  receive</div><div>    {[H | T], _L, CurrentMessage, M} -></div><div>      io:format("~p received ~p~n", [self(), CurrentMessage]),</div><div>      H ! {T, _L, CurrentMessage, M},</div><div>      loop();</div><div>    {[], Ring, CurrentMessage, M} -></div><div>      io:format("~p received ~p with empty list~n", [self(), CurrentMessage]),</div><div>      case CurrentMessage < M of</div><div>        true -></div><div>          [_ | [Next | T]] = Ring,</div><div>          NewMessage = CurrentMessage + 1,</div><div>          io:format("sending message ~p to ~p~n", [NewMessage, Next]),</div><div>          Next ! {T, Ring, NewMessage, M};</div><div>        false -> io:format("done sending ~p messages in ~p ring, taking rest now.~n",[M, Ring])</div><div>      end,</div><div>      loop()</div><div>  end.</div></div><div><br></div><div><b><u>Output</u></b><br></div><div><div>1> ring:message(4, 3).</div><div><0.33.0> received 1</div><div>{[<0.34.0>,<0.35.0>,<0.36.0>,<0.33.0>],</div><div> [<0.33.0>,<0.34.0>,<0.35.0>,<0.36.0>,<0.33.0>],</div><div> 1,3}</div><div><0.34.0> received 1</div><div><0.35.0> received 1</div><div>2> <0.36.0> received 1</div><div>2> <0.33.0> received 1 with empty list</div><div>2> sending message 2 to <0.34.0></div><div>2> <0.34.0> received 2</div><div>2> <0.35.0> received 2</div><div>2> <0.36.0> received 2</div><div>2> <0.33.0> received 2 with empty list</div><div>2> sending message 3 to <0.34.0></div><div>2> <0.34.0> received 3</div><div>2> <0.35.0> received 3</div><div>2> <0.36.0> received 3</div><div>2> <0.33.0> received 3 with empty list</div><div>2> done sending 3 messages in [<0.33.0>,<0.34.0>,<0.35.0>,<0.36.0>,<0.33.0>] ring, taking rest now.</div><div>2> </div></div></div>