[erlang-questions] eep: multiple patterns

Sean Hinde sean.hinde@REDACTED
Sat May 31 01:52:41 CEST 2008


You are right.

I had thought that there was an optimisation where if the pattern was  
identical to last time the same receive was entered for that process  
then only newer messages were examined.

Seems like it could be a useful optimisation in any case :-)

Sean

On 30 May 2008, at 17:07, Per Melin wrote:

> 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}




More information about the erlang-questions mailing list