working with binaries

Per Gustafsson per.gustafsson@REDACTED
Tue Jul 11 15:23:01 CEST 2006


Christian S wrote:

> list_to_binary([A,B]) for concatenation.
>
> As for sub binaries, I tend to use pattern matching.
>
> 1> A = <<"ABCDEFGHIJKLM">>.
> <<65,66,67,68,69,70,71,72,73,74,75,76,77>>
> 2> <<_:5/binary,Sub:3/binary,_/binary>> = A.
> <<65,66,67,68,69,70,71,72,73,74,75,76,77>>
> 3> Sub.
> <<70,71,72>>
>
> It would be interesting to hear comments on the pattern matching
> approach. I tend to use the method to index individual bytes from
> binaries too. Is it terribly inefficient or as low constant factor
> O(1) as one can get?
>
Indexing an individual byte in a binary using pattern matching is 
reasonably efficient. It is O(1) and pretty close to using a bif to 
index the binary, in fact if you extract several values in one pattern 
matching statement it is often faster than using a bif to extract the 
values.

Per



More information about the erlang-questions mailing list