Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 21st, 2012  |  syntax: None  |  size: 1.27 KB  |  hits: 9  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. -module(ring_bench).
  2. -export([start/1]).
  3.  
  4. %%  start([N, M])
  5. %%  Create N processes into ring and sned message round the ring M times
  6.  
  7. start([A, B]) ->
  8.     N = list_to_integer(atom_to_list(A)),
  9.     M = list_to_integer(atom_to_list(B)),
  10.     Self = self(),
  11.     Max = erlang:system_info(process_limit),
  12.     io:format("Maximum allowed processes: ~p~n", [Max]),
  13.     statistics(runtime),
  14.     statistics(wall_clock),
  15.     Pids = for(1, N, fun() -> spawn(fun() -> loop(Self, N * M) end) end),
  16.    
  17.     [H | _] = Pids,
  18.     H ! {0, Pids},
  19.    
  20.     receive
  21.         done ->
  22.             lists:foreach(fun(Pid) -> Pid ! die end, Pids),
  23.             {_, Time1} = statistics(runtime),
  24.             {_, Time2} = statistics(wall_clock),
  25.             U1 = Time1 / 1000,
  26.             U2 = Time2 / 1000,
  27.             io:format("Time=~p (~p) seconds~n", [U1, U2]),
  28.             init:stop()
  29.     end.
  30.  
  31. loop(Master, N) ->
  32.     receive
  33.         die ->
  34.             void;
  35.  
  36.         {I, _} when I =:= N->
  37.             Master ! done;
  38.  
  39.         {I, Pids} ->
  40.             [H | T] = Pids,
  41.             NextPids = lists:reverse([H | lists:reverse(T)]),
  42. %            io:format("~p ~p ~p ~n", [I, self(), NextPids]),
  43.             H ! {I+1, NextPids},
  44.             loop(Master, N)
  45.     end.
  46.  
  47. for(N, N, F) -> [F()];
  48. for(I, N, F) -> [F()|for(I+1, N, F)].