不過我分析了這個測試裡的erlang代碼,存在問題,并沒有完成所有的循環,程序就結束了,這對比較結果有較大的影響。原始代碼如下:
我将process修改一下:
然後執行下zog:run(3,3),你将發現消息繞了兩圈就結束了,第三圈根本沒有進行,不知道測試者是什麼用意。依照現在的執行300萬次消息傳送竟然隻需要3毫秒!我修改了下了下代碼如下:
-module(zog).
%% this is a test program that first creates n processes (that are
%% "connected" in a ring) and then sends m messages in that ring.
%%
%% - september 1998
%% - roland
-export([start/0, start/1, start/2]).
-export([run/2, process/2]). % local exports - ouch
start() -> start(16000).
start(n) -> start(n, 1000000).
start(n, m) -> spawn(?module, run, [n, m]).
run(n, m) when n < 1 ->
io:format("must be at least 1 process~n", []),
0.0;
run(n, m) ->
statistics(wall_clock),
limit=n-n*m+1+m,
pid = setup(n-1,limit,self()),
{_,t1} = statistics(wall_clock),
io:format("setup : ~w s", [t1/1000]),
case n of
1 -> io:format(" (0 spawns)~n", []);
_ -> io:format(" (~w us per spawn) (~w spawns)~n",
[1000*t1/(n-1), n-1])
end,
% io:format("run's pid=~w~n",[pid]),
pid ! m,
k = process(pid,limit),
% io:format("run's k=~w~n",[k]),
{_,t2} = statistics(wall_clock),
time = 1000*t2/(m+k),
io:format("run : ~w s (~w us per msg) (~w msgs)~n",
[t2/1000, time, (m+k)]),
t2/1000.
setup(0,limit, oldpid) ->
oldpid;
setup(n,limit, oldpid) ->
newpid = spawn(?module, process, [oldpid,limit]),
setup(n-1, limit,newpid).
process(pid,limit) ->
receive
m ->
pid ! m-1,
% io:format("from ~w to ~w and m=~w~n",[self(),pid,m]),
if
m <limit -> -m;
true -> process(pid,limit)
end
end.
文章轉自莊周夢蝶 ,原文釋出時間2007-08-04