<div dir="ltr"><div>A much better way than sending the path around is for every process to keep track for itself the next process in the ring so you just need to send the message between the processes. At least if you are after speed. If you do do need to be able to dynamically restructure the ring it would be easy to add an extra message where you tell a process its new next process.<br><br></div>Robert<br><br></div><div class="gmail_extra"><br><div class="gmail_quote">On 11 January 2015 at 20:48, Harit Himanshu <span dir="ltr"><<a href="mailto:harit.subscriptions@gmail.com" target="_blank">harit.subscriptions@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><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="14ada8afd1ff49a4_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>
<br>_______________________________________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br>
<a href="http://erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/listinfo/erlang-questions</a><br>
<br></blockquote></div><br></div>