Thanks for the tips. I'm going to start using this idiom now.<br><br>BTW, is the Erlang VM capable of expanding a binary without copying data if there is a contiguous block of memory available? If a feature like this one could be added, it would be possible to efficiently append data to a binary without resorting to using a list as an intermediate value. Another option could be to make the binaries preallocate memory.
<br><br>Just a thought...<br><br><br><br><div><span class="gmail_quote">On 15 Nov 2007 07:53:26 +0100, <b class="gmail_sendername">Bjorn Gustavsson</b> <<a href="mailto:bjorn@erix.ericsson.se">bjorn@erix.ericsson.se</a>
> wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">"Juan Jose Comellas" <<a href="mailto:juanjo@comellas.org">juanjo@comellas.org
</a>> writes:<br><br>> One thing that I was always doing when iterating over binaries is doing<br>> matches like the following one:<br>><br>> <<Head:Offset/binary, Char, Tail/binary>><br><br>You don't have to do that anymore in R12B.
<br><br>Take for instance your index/2 function:<br><br>index(Str, Char) when is_binary(Str), is_integer(Char) -><br> index(Str, Char, 0).<br>index(Str, _Char, N) when N > size(Str) -><br> 0;<br>index(Str, Char, N) ->
<br> case Str of<br> <<_Head:N/binary, Char, _Tail/binary>> -><br> N + 1;<br> <<>> -><br> 0;<br> _ -><br> index(Str, Char, N + 1)<br>
end.<br><br>In R12B, you can write it simply like:<br><br>index(Str, Char) when is_binary(Str), is_integer(Char) -><br> index(Str, Char, 1).<br><br>index(<<Char,T/binary>>, Char, N) -><br> N;<br>
index(<<_,T/binary>>, Char, N) -><br> index(T, Char, N+1);<br>index(<<>>, _, _) -> 0.<br><br>On my Sparc workstation this simpler program is more than twice as fast<br>in R12B.<br><br>Note: Your index function can be written speeded a bit.
<br><br>index(Str, Char) when is_binary(Str), is_integer(Char) -><br> index(Str, Char, 0).<br>%% index(Str, _Char, N) when N > size(Str) -><br>%% 0;<br>index(Str, Char, N) -><br> case Str of<br> <<_Head:N/binary, Char, _Tail/binary>> ->
<br> N + 1;<br> <<_Head:N/binary>> -><br> 0;<br> _ -><br> index(Str, Char, N + 1)<br> end.<br><br>Still, even after this improvement, in R12B the simpler version is about twice
<br>as fast.<br><br>/Bjorn<br>--<br>Björn Gustavsson, Erlang/OTP, Ericsson AB<br></blockquote></div><br>