[erlang-questions] strings vs binaries

Joe Armstrong erlang@REDACTED
Thu Aug 20 18:24:52 CEST 2015


On Tue, Aug 18, 2015 at 10:23 PM, Rick Pettit <rpettit@REDACTED> wrote:
> Generally speaking, you probably want to use binaries these days as they
> consume far less memory (at least for “large” strings):

Right - but with the caveat that most string's aren't "large".

As an example the source code of the text of my Erlang book (27
chapters and 4 appendices) is 1.22 MBytes of XML text. At 16
bytes/byte overhead
this is 19.5 MBytes - so I could store 50 books in 1GByte - I have 8GBytes
of memory enough for 400 books.

I have often parsed all this XML and manipulated (as strings) it in
the twinking of an eye. So this notion of "small" must be in relation
to what you want to do.

My rule of thumb is that any text I have entered by hand
is "small" - including entire books and all the code I have every written.

I use binaries as memory buffers and when I need to know the exact
memory layout (in terms of bits and bytes) - I use lists when I want to
perform symbolic manipulation of data.

What's big and small and whether to use lists or binaries depends upon
what you want to do - at the end of the day the choice should be based
on
measurements not guesswork.

Cheers

/Joe





>
>   http://www.erlang.org/doc/efficiency_guide/advanced.html
>
> That goes for both sending over the wire as well as internally.
>
> -Rick
>
> On Aug 18, 2015, at 3:16 PM, Ben Hsu <benhsu@REDACTED> wrote:
>
> Hello
>
> I have a simple question, I know Erlang has strings and binary strings as
> separate data types, and they're different even if they "look the same" in
> the console
>
>
> 1> io:format(<<"fnord">>).
> fnord
> 2> io:format("fnord").
> fnord
>
> 3> "fnord"==<<"fnord">>
> 3> .
> false
>
> My question is when you will use each one. Are binary strings used for
> sending data over the wire, and normal strings used internally? what are the
> tradeoffs?
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
>
>
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
>



More information about the erlang-questions mailing list