[erlang-questions] eep: multiple patterns
Christopher Atkins
christopher316@REDACTED
Sat May 31 03:02:45 CEST 2008
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?
-module(test_receive).
-compile(export_all).
start() ->
statistics(runtime),
statistics(wall_clock),
PidLoop1 = spawn(?MODULE, loop1,[]),
sender(PidLoop1, 10000000),
{_, Loop1Time1} = statistics(runtime),
{_, Loop1Time2} = statistics(wall_clock),
io:format("Sent ~p messages in ~p /~p~n", [100000, Loop1Time1,
Loop1Time2]),
statistics(runtime),
statistics(wall_clock),
PidLoop2 = spawn(?MODULE, loop2,[]),
sender(PidLoop2, 10000000),
{_, Loop2Time1} = statistics(runtime),
{_, Loop2Time2} = statistics(wall_clock),
io:format("Sent ~p messages in ~p /~p~n", [100000, Loop2Time1,
Loop2Time2]).
sender(_, 0) -> void;
sender(Pid, N) ->
if
N rem 2 =:= 2 ->
Pid ! test2;
true ->
Pid ! test1
end,
sender(Pid, N - 1).
proc1(F) ->
receive
start -> spawn_link(F)
end.
loop1() ->
receive
%%test1 -> loop1();
test2 -> loop1()
end.
loop2() ->
receive
_ -> loop2()
end.
------------------------------------------------------------------------------------------------------------------------------
Message: 2
Date: Fri, 30 May 2008 18:07:18 +0200
From: "Per Melin" <per.melin@REDACTED>
Subject: Re: [erlang-questions] eep: multiple patterns
To: "Sean Hinde" <sean.hinde@REDACTED>
Cc: Erlang Questions <erlang-questions@REDACTED>
Message-ID:
<d57f8dd90805300907u44fe7316x2421ab28632cf633@REDACTED>
Content-Type: text/plain; charset=ISO-8859-1
2008/5/30 Per Melin <per.melin@REDACTED>:
> If I send 100k 'foo' messages and then 100k 'bar' messages to a
> process, and then do a catch-all receive until there are no messages
> left, that takes 0.03 seconds.
>
> If I do a selective receive of only the 'bar' messages, it takes 90
seconds.
I found my old test code:
-module(selective).
-export([a/2, c/2]).
a(Atom, N) ->
spawn(fun() -> b(Atom, N) end).
b(Atom, N) ->
spam_me(foo, N),
spam_me(bar, N),
R = timer:tc(?MODULE, c, [Atom, N]),
io:format("TC: ~p~n", [R]).
c(Atom, N) ->
receive
Atom -> c(Atom, N - 1)
after 0 ->
N
end.
spam_me(Msg, Copies) ->
lists:foreach(fun(_) -> self() ! Msg end, lists:duplicate(Copies, 0)).
---
2> selective:a(bar, 100000).
<0.38.0>
TC: {124130689,0}
3> selective:a(foo, 100000).
<0.40.0>
TC: {23176,0}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20080530/c77a0c01/attachment.htm>
More information about the erlang-questions
mailing list