[erlang-questions] how much time required to pass messages.

Vikrant vikrant.patil@REDACTED
Sun Mar 29 11:33:41 CEST 2009


Hi,
    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.

26> tokenring:tokenring(10000,1).
Time required to pass message 10000 times=3.00000 (4.40000) microsecond
.ok
27> tokenring:tokenring(20000,1).
Time required to pass message 20000 times=3.00000 (4.65000) microsecond
.ok
28> tokenring:tokenring(30000,1).
Time required to pass message 30000 times=3.33333 (4.63333) microsecond
.ok

(time in bracket is system time and time outside bracket is user time in
microseconds.)

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.

But then look at the timings when I vary M keeping N constant.

29> tokenring:tokenring(30000,1).
Time required to pass message 30000 times=3.33333 (4.63333) microsecond
.ok
30> tokenring:tokenring(30000,2).
Time required to pass message 60000 times=7.00000 (8.93333) microsecond
.ok
31> tokenring:tokenring(30000,3).
Time required to pass message 90000 times=11.0000 (14.6667) microsecond
.ok
32> tokenring:tokenring(30000,4).
Time required to pass message 120000 times=13.6667 (20.9667) microsecond
.ok


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.

can somebody explain me this discrepancy? why this time is proportional to M
but not proportional to N?

code for above program is as given below


%%%-------------------------------------------------------------------
%%% File    : tokenring.erl
%%% Author  : vikrant <vikrant@REDACTED>
%%% Description : this module implements token ring protocol for
benchmarking
%%%
%%% Created : 28 Mar 2009 by vikrant <vikrant@REDACTED>
%%%-------------------------------------------------------------------
-module(tokenring).
-export([tokenring/2]).


tokenring(N,M)->
    [Head | Tail] = for(1,N, fun()-> spawn(fun()-> wait() end) end),
    TransposeByOne = lists:append(Tail,[Head]),
    Pairs = lists:zip([Head | Tail], TransposeByOne),
    lists:map(fun({To, Arg})-> To! {token, Arg, M*N} end, Pairs),
    statistics(runtime),
    statistics(wall_clock),
    Head ! {token, self(), 0},
    receive
    done ->
        void
    end,
    {_,Time1} = statistics(runtime),
    {_,Time2} = statistics(wall_clock),
    U1 = Time1*1000/N,
    U2 = Time2*1000/N,
    lists:foreach(fun(Pid)-> Pid!die end, [Head | Tail]),
    io:format("Time required to pass message ~p times=~p (~p)
microsecond~n.",
          [M*N,U1,U2]).


loop(To, MaxCount) ->
    receive
    {token,Parent,Count} when Count < MaxCount ->
        %io:format("Token passing from ~p to ~p~n",[self(), To]),
        To! {token, Parent, Count+1},
        loop(To, MaxCount);
    {token,Parent, MaxCount} ->
        Parent ! done,
        loop(To, MaxCount);
    die ->
        void
    end.


wait() ->
    receive
    {token, To, MaxCount} ->
        loop(To, MaxCount)
    end.

for(N,N,F) -> [F()];
for(I,N,F) -> [F() | for(I+1,N,F)].


Thanks and Regards,
(Vikrant)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20090329/ec49e30f/attachment.htm>


More information about the erlang-questions mailing list