binaries vs lists

Bjorn Gustavsson bjorn@REDACTED
Tue Nov 11 17:08:04 CET 2003


concat_binary/1 is definitely inefficient if done for each byte.
You should build a list instead.

/Bjorn


Serge Aleynikov <serge@REDACTED> writes:

> Hi!
> 
> I was wonderting if someone could contribute a thought to the
> following question regarding efficiency.
> 
> I have an Erlang TCP client that processes a binary stream which needs
> to be post-processed by removing escaped bytes.  Let's say, that byte
> 16$FF is escaped as <<16#FE, 16$01>>, and the 16#FF value is used as a
> message separator.  The variable hex messages are within 512 bytes
> each.
> 
> What would be faster for Erlang:
> 
> 1. Declare a socket to return a *list* of bytes, and do something like:
> 
> unescape([], Msg)             -> {lists:reverse(Msg), []};
> unescape([16#FE, 1 | T], Msg) -> unescape(T, [16#FF | Msg]);
> unescape([16#FF | Bytes]=NextMsg, Msg) ->
>      {lists:reverse(Msg), NextMsg};
> unescape([H | T], Msg)        -> unescape(T, [H | Msg]);
> 
> 2. Alternatively, declare a socket to return *binaries*, and do:
> 
> unescape(<<>>, Msg) -> {binary_to_list(Msg), <<>>};
> unescape(<<16#FE, 1, T>>, Msg) ->
>      unescape(T, concat_binary([Msg, 16#FF]));
> unescape(<<16#FF, Bytes>> = NextMsg, Msg) ->
>      {binary_to_list(Msg), NextMsg};
> unescape(<<H, T>>, Msg) ->
>      unescape(T, concat_binary([Msg, H]));
> 
> Intuitively I think that the binary approach should work faster, but I
> want to make sure that concat_binary is not expensive to do for every
> byte in a stream.
> 
> Thanks.
> 
> Serge
> 
> 

-- 
Björn Gustavsson            Ericsson Utvecklings AB
bjorn@REDACTED      ÄT2/UAB/F/P
			    BOX 1505
+46 8 727 56 87 	    125 25 Älvsjö



More information about the erlang-questions mailing list