Hello, I tried (poorly--I'm a complete novice) to implement a benchmark from your earlier statement. I didn't do the same thing (load up the message mailbox before consuming them), but what I did write led to a perplexing (to me) discovery. If I uncomment the line in [loop1/0] below, performance for that loop degrades by an order of magnitude. Why is that?<br>
<br>-module(test_receive).<br>-compile(export_all).<br><br>start() -><br> statistics(runtime),<br> statistics(wall_clock),<br> PidLoop1 = spawn(?MODULE, loop1,[]),<br> sender(PidLoop1, 10000000),<br>
{_, Loop1Time1} = statistics(runtime),<br> {_, Loop1Time2} = statistics(wall_clock),<br> io:format("Sent ~p messages in ~p /~p~n", [100000, Loop1Time1, Loop1Time2]),<br> statistics(runtime),<br>
statistics(wall_clock),<br> PidLoop2 = spawn(?MODULE, loop2,[]),<br> sender(PidLoop2, 10000000),<br> {_, Loop2Time1} = statistics(runtime),<br> {_, Loop2Time2} = statistics(wall_clock),<br>
io:format("Sent ~p messages in ~p /~p~n", [100000, Loop2Time1, Loop2Time2]).<br><br>sender(_, 0) -> void;<br>sender(Pid, N) -><br> if<br> N rem 2 =:= 2 -><br> Pid ! test2;<br>
true -><br> Pid ! test1<br> end,<br> sender(Pid, N - 1).<br><br>proc1(F) -><br> receive<br> start -> spawn_link(F)<br> end.<br><br>loop1() -><br>
receive<br> %%test1 -> loop1();<br> test2 -> loop1()<br> end.<br><br>loop2() -><br> receive<br> _ -> loop2()<br> end.<br><br><br>------------------------------------------------------------------------------------------------------------------------------<br>
Message: 2<br>
Date: Fri, 30 May 2008 18:07:18 +0200<br>
From: "Per Melin" <<a href="mailto:per.melin@gmail.com">per.melin@gmail.com</a>><br>
Subject: Re: [erlang-questions] eep: multiple patterns<br>
To: "Sean Hinde" <<a href="mailto:sean.hinde@gmail.com">sean.hinde@gmail.com</a>><br>
Cc: Erlang Questions <<a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a>><br>
Message-ID:<br>
<<a href="mailto:d57f8dd90805300907u44fe7316x2421ab28632cf633@mail.gmail.com">d57f8dd90805300907u44fe7316x2421ab28632cf633@mail.gmail.com</a>><br>
Content-Type: text/plain; charset=ISO-8859-1<br>
<br>
2008/5/30 Per Melin <<a href="mailto:per.melin@gmail.com">per.melin@gmail.com</a>>:<br>
> If I send 100k 'foo' messages and then 100k 'bar' messages to a<br>
> process, and then do a catch-all receive until there are no messages<br>
> left, that takes 0.03 seconds.<br>
><br>
> If I do a selective receive of only the 'bar' messages, it takes 90 seconds.<br>
<br>
I found my old test code:<br>
<br>
-module(selective).<br>
<br>
-export([a/2, c/2]).<br>
<br>
a(Atom, N) -><br>
spawn(fun() -> b(Atom, N) end).<br>
<br>
b(Atom, N) -><br>
spam_me(foo, N),<br>
spam_me(bar, N),<br>
R = timer:tc(?MODULE, c, [Atom, N]),<br>
io:format("TC: ~p~n", [R]).<br>
<br>
c(Atom, N) -><br>
receive<br>
Atom -> c(Atom, N - 1)<br>
after 0 -><br>
N<br>
end.<br>
<br>
spam_me(Msg, Copies) -><br>
lists:foreach(fun(_) -> self() ! Msg end, lists:duplicate(Copies, 0)).<br>
<br>
---<br>
<br>
2> selective:a(bar, 100000).<br>
<0.38.0><br>
TC: {124130689,0}<br>
3> selective:a(foo, 100000).<br>
<0.40.0><br>
TC: {23176,0}<br><br>