-module(pingpong). -export([oneway/0, consumer/0, pingpong/0]). oneway() -> N = 10000000, Pid = spawn(?MODULE, consumer, []), Start = erlang:now(), dotimes(N - 1, fun () -> Pid ! message end), Pid ! {done, self()}, receive ok -> ok end, Stop = erlang:now(), N / time_diff(Start, Stop). pingpong() -> N = 10000000, Pid = spawn(?MODULE, consumer, []), Start = erlang:now(), Message = {ping, self()}, dotimes(N, fun () -> Pid ! Message, receive pong -> ok end end), Stop = erlang:now(), N / time_diff(Start, Stop). consumer() -> receive message -> consumer(); {done, Pid} -> Pid ! ok; {ping, Pid} -> Pid ! pong, consumer() end. dotimes(0, _) -> done; dotimes(N, F) -> F(), dotimes(N - 1, F). time_diff({A1,A2,A3}, {B1,B2,B3}) -> (B1 - A1) * 1000000 + (B2 - A2) + (B3 - A3) / 1000000.0 .