Skip to content

Instantly share code, notes, and snippets.

@auser
Created January 23, 2010 04:54
Show Gist options
  • Save auser/284444 to your computer and use it in GitHub Desktop.
Save auser/284444 to your computer and use it in GitHub Desktop.
-module(p81).
-compile(export_all).
bench(ProcN, MsgN) ->
ProcRing = for(1, ProcN, fun() -> spawn(fun wait/0) end),
statistics(runtime),
statistics(wall_clock),
sendMsgs(ProcRing, ping, ProcN*MsgN),
{_, Time1} = statistics(runtime),
{_, Time2} = statistics(wall_clock),
U1 = Time1 * 1000 / ProcN,
U2 = Time2 * 1000 / ProcN,
io:format("Process spawn time=~p (~p) microseconds~n", [U1, U2]),
%% do I need to kill them afterwards or will the gc do it automatically?
lists:foreach(fun(Pid) -> Pid ! die end, ProcRing).
wait() ->
receive
die -> void;
ping -> wait()
end.
% for creating our processes
for(N, N, F) -> [F()];
for(I, N, F) -> [F()|for(I+1, N, F)].
sendMsgs(_, _, 0) -> ok;
sendMsgs(ProcList, Msg, MsgN) ->
lists:foreach(fun(Pid) -> Pid ! Msg end, ProcList),
sendMsgs(ProcList, Msg, MsgN-length(ProcList)).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment