1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| -module(pingpong). -mode(compile). -compile(export_all). -compile(nowarn_export_all).
send_msg(ID, Msg) -> Msg, ID ! {self()}, ok.
pong(0) -> ok; pong(N) -> receive {PingID} -> send_msg(PingID, "Pong"), pong(N-1) end.
ping(_, 0) -> ok; ping(PongID, N) -> send_msg(PongID, "Ping"), receive _ -> ping(PongID, N-1) end.
main(_) -> MaxCount = 20000, PongID = spawn(?MODULE, pong, [MaxCount]), T1 = erlang:monotonic_time(), ping(PongID, MaxCount), T2 = erlang:monotonic_time(), Time = erlang:convert_time_unit(T2 - T1, native, microsecond), io:format("Elapsed time: ~p ms~n", [Time / 1000]), ok.
|