If you want to do faster line splitting you might like to use lists instead of binaries<br><br>I timed 5 different ways of splitting a file into lines<br><br>The list version was the fastest - see enclosure.<br><br>/Joe<br>
<br><br><div><span class="gmail_quote">On 22 Sep 2006 09:10:32 +0200, <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;">
I know that it isn't obvious, but the following version of your<br>function should be faster:<br><br>break_on_nl1(B) -> break_on_nl1(0, B).<br>break_on_nl1(Len, Bin) when Len =:= size(Bin) -><br>        {Bin, <<>>};
<br>break_on_nl1(Len, Bin) -><br>    case Bin of<br>        <<_:Len/binary, $\n, _/binary>> -><br>            <<Msg:Len/binary, _, Tail/binary>> = Bin,<br>            {Msg, Tail};<br>        _ ->
<br>            break_on_nl1(Len+1, Bin)<br>    end.<br><br>I haven't done any measurements on execution times, but I know<br>that the revised version avoids creating two sub-binaries, which<br>should make it faster.<br><br>
We hope to be able to add better optimization to a future version<br>of the Erlang compiler, so that you will not have to write such<br>contrived code to get the fastest possible bit syntax matching.<br><br>/Bjorn<br><br>
Gaspar Chilingarov <<a href="mailto:nm@web.am">nm@web.am</a>> writes:<br><br>> Hi all!<br>><br>> I wish to share an experience which I had today when writing code.<br>><br>[...]<br>><br>> break_on_nl1(B) -> break_on_nl1(0, B).
<br>> break_on_nl1(Len, Bin) when Len == size(Bin) -><br>>       {Bin, <<>>};<br>> break_on_nl1(Len, Bin) -><br>>       <<Msg:Len/binary, Symb, Tail/binary>> = Bin,<br>>       case Symb of
<br>>               $\n -> {Msg, Tail};<br>>               _ -> break_on_nl1(Len+1, Bin)<br>>       end.<br><br>--<br>Björn Gustavsson, Erlang/OTP, Ericsson AB<br><br>_______________________________________________
<br>erlang-questions mailing list<br><a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br><a href="http://www.erlang.org/mailman/listinfo/erlang-questions">http://www.erlang.org/mailman/listinfo/erlang-questions
</a><br></blockquote></div><br>