[erlang-questions] Basic question about Erlang binaries

Igor Ribeiro Sucupira igorrs@REDACTED
Sat Sep 26 09:04:27 CEST 2009


On Sat, Sep 26, 2009 at 3:25 AM, Igor Ribeiro Sucupira <igorrs@REDACTED> wrote:
> On Sat, Sep 26, 2009 at 1:29 AM, Sean Cribbs <seancribbs@REDACTED> wrote:
>
> If it was just pointer arithmetic, it would be constant (not
> proportional to the offset). But, as I said, it doesn't seem to be.
>
> Try this in the shell:
>
> Binary = term_to_binary(lists:seq(1, 1000000)).
> lists:foreach(fun(_) -> <<_:7, Bit:1, _/binary>> = Binary end,
> lists:seq(1, 1000000)).
> lists:foreach(fun(_) -> <<_:1048575, Bit:1, _/binary>> = Binary end,
> lists:seq(1, 1000000)).
>
> The second statement takes some seconds to run, while the third one
> will take forever (of course I haven't waited  :-)).


Hum... this one below finishes fast.  :-)

lists:foreach(fun(_) -> <<_:1048568/binary, Bit:8, _/binary>> = Binary
end, lists:seq(1, 1000000)).

What happened in the other example (with _:1048575)? Is some
validation taking place there to make sure an integer matches the
initial portion of Binary (?)?

Just curious.  :-)

Thanks.
Igor.


More information about the erlang-questions mailing list