[erlang-questions] Updating Myths of Erlang Performance

Björn Gustavsson bjorn@REDACTED
Sat Jun 4 09:41:17 CEST 2016


On Sat, Jun 4, 2016 at 9:07 AM, Valentin Micic <valentin@REDACTED> wrote:
>
> On 04 Jun 2016, at 8:13 AM, Björn Gustavsson wrote:
>
>> "I thought binaries were always faster than lists, so why is binary
>> matching slower than list matching?"
>>
>
> Stated this way, the myth may be confusing to some people (well, it is for me).
> So, what is the myth? Is it that binaries are faster than lists,  or that binary matching is slower then list matching?
>

Yes, the myth needs some polishing.

Here are some more details of what I had in mind.

It is often recommended to use binaries (for example in this mailing)
instead of lists.

Someone read this recommendation, tries it, and is then surprised that
this code:

f(<<B:/8,T/binary>>) ->
   %% Do something with B.
   f(T);
f(<<>>) ->
   %% Return something.

is slower than:

f([B|T]) ->
   %% Do something with B.
   f(T);
f([]) ->
   %% Return something.

Fact is, it is hard to beat the speed of lists, and that can be surprising.

When you do more complicated matching such as:

f(<<B:/32,T/binary>>) ->
   %% Do something with B.
   f(T);
f(<<>>) ->
   %% Return something.

instead of:

f([A,B,C,D|T]) ->
   X = (A bsl 24) bor (B bsl 16) bor (C bsl 8) bor D,
   %% Do something with X.
   f(T);
f([]) ->
   %% Return something.

bit syntax matching wins.

/Björn

-- 
Björn Gustavsson, Erlang/OTP, Ericsson AB



More information about the erlang-questions mailing list