[erlang-questions] Updating Myths of Erlang Performance

Loïc Hoguin essen@REDACTED
Sat Jun 4 12:52:32 CEST 2016

On 06/04/2016 09:41 AM, Björn Gustavsson wrote:
> 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.

On the other hand, if your input is a binary, converting + looping might 
be slower than just looping through the binary. Not to mention the extra 
garbage created.

This section should point this out, otherwise we'll end up with users 
converting binaries to list "because lists are faster for this".

Also I think there are too few cases where lists win, especially in that 
kind of comparison. You are basically comparing binaries-as-string vs 
strings, and when using those it's frequent to match against <<"abcdef", 
T/binary>> forms (vs [$a, $b, $c, $d, $e, $f|T]) and as you pointed out, 
in that case binaries win.

IMO the myth is that list strings are efficient at all when in fact 
parsing them or even just having them in memory is inefficient. They 
have their use cases (Unicode related mostly).

Of course if the section is about people storing lists of integers in a 
binary then it makes perfect sense, but I haven't seen that one yet. :-)

Loïc Hoguin
Author of The Erlanger Playbook,
A book about software development using Erlang

More information about the erlang-questions mailing list