《Erlang程序设计》第8章习题2

编写一个环形基准测试,在一个环中创建N个进程,然后沿着环发送一条消息M次,最后总共发送M*N条消息。在N和M的不同取值下测试整个过程消耗时间。

-module(ex2).
-export([start/2]).

start(N,M)->
    statistics(runtime),
    statistics(wall_clock),
    L=create(N),
    post(L,M),
    {_,Time1} = statistics(runtime),
    {_,Time2} = statistics(wall_clock),
    io:format("total time: ~p(~p)ms~n",[Time1,Time2]).




create(N)->
    create(N,[]).

create(0,L)->
    L;
create(N,L)->
    Pid=spawn(fun loop/0),
    create(N-1,[Pid|L]).



loop()->
    receive
    cancel->
        void
end.





post(_,0)->
    void;
post(L,M) ->
    [A ! canel ||A<-L],
    post(L,M-1).
  1. 尚無回應.

  1. 尚無引用.