Hi,<br> I have a program which creates N processes. Assume that processes are arranged in a circular fashion. First process pases a token to neighboring process and that process in turn passes it further. This is continued till the token comes back to first process. I can choose to do this process M times (i.e the message is passed around complete circle M times). I have written a function tokenring(N,M) which creates N processes and circulates the message M times. Now consider instances where I chose N =10000, 20000, 30000 respectively with M keeping constant to 1. I counted time required to do this. and stats looks like this.<br>
<br>26> tokenring:tokenring(10000,1).<br>Time required to pass message 10000 times=3.00000 (4.40000) microsecond<br>.ok<br>27> tokenring:tokenring(20000,1).<br>Time required to pass message 20000 times=3.00000 (4.65000) microsecond<br>
.ok<br>28> tokenring:tokenring(30000,1).<br>Time required to pass message 30000 times=3.33333 (4.63333) microsecond<br>.ok<br><br>(time in bracket is system time and time outside bracket is user time in microseconds.)<br>
<br>This is surprising to me! I expected that time required to pass token along N processes should be proportional to N! because with N increasing, number messages passed increase in proportion to N. But it seems to take same time irrespective of number of processes.<br>
<br>But then look at the timings when I vary M keeping N constant.<br><br>29> tokenring:tokenring(30000,1).<br>Time required to pass message 30000 times=3.33333 (4.63333) microsecond<br>.ok<br>30> tokenring:tokenring(30000,2).<br>
Time required to pass message 60000 times=7.00000 (8.93333) microsecond<br>.ok<br>31> tokenring:tokenring(30000,3).<br>Time required to pass message 90000 times=11.0000 (14.6667) microsecond<br>.ok<br>32> tokenring:tokenring(30000,4).<br>
Time required to pass message 120000 times=13.6667 (20.9667) microsecond<br>.ok<br><br><br>this seems to be proportional to M ..i.e to number of times message is passes around. This case seems to match with my expectations. <br>
<br>can somebody explain me this discrepancy? why this time is proportional to M but not proportional to N?<br><br>code for above program is as given below<br><br><br>%%%-------------------------------------------------------------------<br>
%%% File : tokenring.erl<br>%%% Author : vikrant <vikrant@nile><br>%%% Description : this module implements token ring protocol for benchmarking<br>%%% <br>%%% Created : 28 Mar 2009 by vikrant <vikrant@nile><br>
%%%-------------------------------------------------------------------<br>-module(tokenring).<br>-export([tokenring/2]).<br><br><br>tokenring(N,M)-><br> [Head | Tail] = for(1,N, fun()-> spawn(fun()-> wait() end) end),<br>
TransposeByOne = lists:append(Tail,[Head]),<br> Pairs = lists:zip([Head | Tail], TransposeByOne),<br> lists:map(fun({To, Arg})-> To! {token, Arg, M*N} end, Pairs),<br> statistics(runtime),<br> statistics(wall_clock),<br>
Head ! {token, self(), 0},<br> receive<br> done -><br> void<br> end,<br> {_,Time1} = statistics(runtime),<br> {_,Time2} = statistics(wall_clock),<br> U1 = Time1*1000/N,<br> U2 = Time2*1000/N,<br>
lists:foreach(fun(Pid)-> Pid!die end, [Head | Tail]),<br> io:format("Time required to pass message ~p times=~p (~p) microsecond~n.",<br> [M*N,U1,U2]).<br><br> <br>loop(To, MaxCount) -><br>
receive<br> {token,Parent,Count} when Count < MaxCount -><br> %io:format("Token passing from ~p to ~p~n",[self(), To]),<br> To! {token, Parent, Count+1},<br> loop(To, MaxCount);<br>
{token,Parent, MaxCount} -><br> Parent ! done,<br> loop(To, MaxCount);<br> die -><br> void<br> end.<br><br><br>wait() -><br> receive<br> {token, To, MaxCount} -><br> loop(To, MaxCount)<br>
end.<br><br>for(N,N,F) -> [F()];<br>for(I,N,F) -> [F() | for(I+1,N,F)].<br><br><br>Thanks and Regards,<br>(Vikrant)<br>