Erlang程序设计第8章的习题:
在笔记本上跑start(1000,1000)要0.4秒,而在一台双CPU的服务器上居然要1.3秒。以为是linux上编译erlang时有优化参数没有设好。看README,注意到默认是打开SMP模拟器的,于是试着使用
start(N,M) ->
statistics(wall_clock),
Pids = for(1,N,fun(_X) -> spawn(fun get_msg/0) end),
for(1,M,fun(X)->send_msg(Pids, "message " ++ integer_to_list(X)) end),
{_,Time} = statistics(wall_clock),
io:format("take ~p seconds~n", [Time/1000]),
send_msg(Pids, stop),
ok.
for(N,N,F) ->
[F(N)];
for(I,N,F) ->
[F(I)|for(I+1,N,F)].
get_msg() ->
receive
stop ->
void;
_Any ->
% io:format("got ~p~n", [_Any]),
get_msg()
end.
send_msg([Pid], Msg) -> Pid!Msg;
send_msg([Pid|Pids], Msg) ->
Pid!Msg,
send_msg(Pids, Msg).
在笔记本上跑start(1000,1000)要0.4秒,而在一台双CPU的服务器上居然要1.3秒。以为是linux上编译erlang时有优化参数没有设好。看README,注意到默认是打开SMP模拟器的,于是试着使用
erl -smp disable启动erl shell将smp模拟器关掉,结果CPU占用率从170%下降到100%,时间变成了0.3秒,看来模拟器的效率还不够好啊。