[erlang-questions] Idiom for multiple case matches

Ulf Wiger <>
Fri Nov 7 20:03:07 CET 2008


The significant difference between the two solutions
is really the complexity. The list variant has linear
complexity, whereas the compiler can do better than
that with disjunct function clause patterns. The longer
the list of vowels, the bigger the difference, and the
case where is_vowel(C) -> false is where the difference
is greatest.

Eliminating the redundant function call can be done
by the compiler through inlining.

BR,
Ulf W

2008/11/7 Valentin Micic <>:
> Wouldn't this be a good candidate for a macro?
>
> -define( IS_WOVEL( C ),         lists:member(C,"aeiouy") ).
>
> It would save one function call that just makes another call.
> Maybe you'd like to run it against your benchmark?
>
> V.
> -----Original Message-----
> From: 
> [mailto:] On Behalf Of Ulf Wiger
> Sent: 07 November 2008 07:29 PM
> To: Edwin Fine
> Cc: Erlang Questions; mats cronqvist
> Subject: Re: [erlang-questions] Idiom for multiple case matches
>
> As of OTP R12B, constant terms are not re-created at each
> invocation, but created once at load time.
>
> BR,
> Ulf W
>
> 2008/11/7 Edwin Fine <>:
>> Ulf,
>>
>> Isn't there also more garbage (as in GC) created by Mats' solution?
> Doesn't
>> the list "aeiouy" get created and destroyed every time, or does the
> compiler
>> "know" it is constant and make one constant copy of it that is not GC'ed?
>>
>> Regards,
>> Edwin Fine
>>
>> On Fri, Nov 7, 2008 at 11:52 AM, Ulf Wiger <> wrote:
>>>
>>> It's slower, true, but both are fairly fast*, and the lists:member/2
>>> solution is also very concise.
>>>
>>> * I ran a small benchmark. On the given machine, Robert's
>>> solution took ca 0.05 usec/call, while Mats's solution took
>>> ca 0.09 usec/call (on average, iterating 1000 over the set of
>>> vowels "aeiouyAEIOUY"). Using the smaller set "aeiouy",
>>> Robert's code still took 0.05 usec/call, while Mats's code
>>> used 0.08 usec.
>>>
>>> BR,
>>> Ulf W
>>>
>>> 2008/11/7 Hynek Vychodil <>:
>>> >
>>> >
>>> > On Fri, Nov 7, 2008 at 3:22 PM, mats cronqvist <>
>>> > wrote:
>>> >>
>>> >> "Robert Virding" <> writes:
>>> >>
>>> >> > A reasonable way of doing it would be:
>>> >> >
>>> >> > case is_vowel(Char) of
>>> >> >     true -> ... ;
>>> >> >     false -> ...
>>> >> > end
>>> >>
>>> >>
>>> >> is_wovel(C) -> lists:member(C,"aeiouy").
>>> >
>>> > It is slower than Robert's solution. If performance is significant
>>> > lists:member solution is worse.
>>> >>
>>> >>
>>> >>  mats
>>> >> _______________________________________________
>>> >> erlang-questions mailing list
>>> >> 
>>> >> http://www.erlang.org/mailman/listinfo/erlang-questions
>>> >
>>> >
>>> >
>>> > --
>>> > --Hynek (Pichi) Vychodil
>>> >
>>> > _______________________________________________
>>> > erlang-questions mailing list
>>> > 
>>> > http://www.erlang.org/mailman/listinfo/erlang-questions
>>> >
>>> _______________________________________________
>>> erlang-questions mailing list
>>> 
>>> http://www.erlang.org/mailman/listinfo/erlang-questions
>>>
>>
>>
> _______________________________________________
> erlang-questions mailing list
> 
> http://www.erlang.org/mailman/listinfo/erlang-questions
>
> _______________________________________________
> erlang-questions mailing list
> 
> http://www.erlang.org/mailman/listinfo/erlang-questions
>



More information about the erlang-questions mailing list