[erlang-questions] Strings usage caveats Mon Mar 5 18:01:39 CET
2012 In a nut-shell a string in Erlang is represented as a list
of (ASCII) characters. So "hello" becomes:
[104,101,108,108,111]. This has many advantages in terms of been
able to process strings. But there are problems: 1) It can take
up lots of memory. A list is 1 word + 1 word for each element +
size of element. So "hello" would (on a 64 bit machine) be 53
bytes. 2) Many of the modules are implemented in Erlang (rather
than in a BIF). Doing extensive string manipulation this way
*could* be slow (when compared to C or other languages.
Fortunately if you need performance you can represent strings as
binaries (I personally think that we should be thinking strings
as binaries all the time now). So the string "hello" would
become <<"hello">> as a binary. The memory efficiency is much
better than with lists (for anything over a few 10's of bytes
it's pretty much "native" size - there is a small overhead IRC).
Better still you can use the very fast b