[erlang-questions] pattern matching vs proplists vs dict performance

Mikhail Sobolev mikhail.sobolev@REDACTED
Thu May 5 21:04:23 CEST 2011


Hi Loïc,

On 5 May 2011 21:14, Loïc Hoguin <essen@REDACTED> wrote:
> On 05/05/2011 10:37 AM, Mikhail Sobolev wrote:
>> We have 10s of thousands of documents that require this kind of
>> conversion and this conversion has to be done fast.  So I decided to
>> write a little benchmark[1], which gave the definitive answer: pattern
>> matching is much faster (times on the test machine):
>>   * proplists -- ~4.9s
>>   * dicts -- ~3.0s
>>   * case inside the function -- ~0.86s
>>   * case as a separate function -- ~0.89s
>>
>> We have discussed the results and started to wonder:
>>
>> 1) are we doing something wrong? (we are all newbies in here)
>
> You should also try:
>  {atom, Value} lists:keyfind(atom, 1, List).
Thanks for the suggestion.  I tried it and it indeed wins over
proplists.  But loses to dict and the rest.

> And, possibly with a little more processing:
>  atom_to_list(atom).
Could you please elaborate?  Why converting an atom to a list might help?

> But pattern matching should still be faster because it can be optimized
> at compile-time.
Aha.

>> 2) why there's such a big difference in time? (proplists module
>> provides BIFs, right?)
>
> No. But lists:keyfind should be a BIF. The proplists module isn't
> designed to be fast.
Thank you.  So our understanding was wrong.  Good to know.

>> 3) are there any other ways to do what we want?
>>
>> The current inclination is to generate a function based on the mapping
>> and then utilize the produced function to do the job.
>
> If your keys are known in advance you should definitely use pattern
> matching.
In a sense, yes, they are.  So we'll go with this approach.

--
Misha



More information about the erlang-questions mailing list