[erlang-questions] Updating Myths of Erlang Performance

Max Lapshin max.lapshin@REDACTED
Sat Jun 4 15:31:47 CEST 2016


I can tell a wonderful example of how _leaving_ integers in compressed form
in binary could speedup our code from 1000 milliseconds per request to 5
milliseconds

But it is is a complicated story because it is about unpacking compressed
structure vs working with packed representation.

On Sat, Jun 4, 2016 at 1:52 PM, Loïc Hoguin <essen@REDACTED> wrote:

> 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
> http://ninenines.eu
> Author of The Erlanger Playbook,
> A book about software development using Erlang
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20160604/55945493/attachment.htm>


More information about the erlang-questions mailing list