Erlang本、やっとp.125まできた
練習問題、30分だけ、やってみよう。
とかいって、嘘。ぜんぜんできなかった。statisticsの挙動が把握できてないし。(追記:Young risk taker.: [Erlang] 実行時間計測を読もう→俺)
とりあえず(2)のほうのコードのメモ。
-module(bead). -compile(export_all). start(N,M) -> P = spawn(fun() -> setup(self()) end), P ! {self(), setup, N}, statistics(runtime), statistics(wall_clock), io:format("kick off~n",[]), P ! {self(), kick, M, M}. setup(PPid) -> receive {Pid, setup, 0} -> io:format("Received:~p~n",[Pid]), PPid ! {ok}, loop(PPid); {_Pid, setup, A} -> Nxtp = spawn(fun() -> setup(PPid) end), Nxtp ! {PPid, setup, A - 1}, loop(Nxtp); Any -> io:format("ignored:~p~n",[Any]), setup(PPid) end. loop(Nxtp) -> receive {_Pid, kick, 0, Total} -> {_, Time1} = statistics(runtime), {_, Time2} = statistics(wall_clock), U1 = Time1 * 1000 / Total, U2 = Time2 * 1000 / Total, io:format("mean passing time ~p usec (~p) usec)~n",[U1,U2]); {_Pid, kick, M, Total} -> Nxtp ! {self(), kick, M - 1, Total}, loop(Nxtp) end.
実行してみると、こんな風景。
1> c(bead). {ok,bead} 2> bead:start(1000,10000). kikk off {<0.31.0>,kick,10000,10000} Received:<0.38.0> mean passing time 1.00000 usec (1.90000) usec) 3> bead:start(1000,1000000). kick off {<0.31.0>,kick,1000000,1000000} Received:<0.1040.0> mean passing time 7.00000e-2 usec (0.540000) usec)
でも、これって、誰のプロセスのcputimeを測ってるんだろうか?
この場合、頼りになるのはwall_clockのelapsed timeだけか?