[erlang-questions] Speeding up string matching

Joe Armstrong <>
Wed Sep 27 15:16:16 CEST 2006


If you want to do faster line splitting you might like to use lists instead
of binaries

I timed 5 different ways of splitting a file into lines

The list version was the fastest - see enclosure.

/Joe


On 22 Sep 2006 09:10:32 +0200, Bjorn Gustavsson <>
wrote:
>
> I know that it isn't obvious, but the following version of your
> function should be faster:
>
> break_on_nl1(B) -> break_on_nl1(0, B).
> break_on_nl1(Len, Bin) when Len =:= size(Bin) ->
>         {Bin, <<>>};
> break_on_nl1(Len, Bin) ->
>     case Bin of
>         <<_:Len/binary, $\n, _/binary>> ->
>             <<Msg:Len/binary, _, Tail/binary>> = Bin,
>             {Msg, Tail};
>         _ ->
>             break_on_nl1(Len+1, Bin)
>     end.
>
> I haven't done any measurements on execution times, but I know
> that the revised version avoids creating two sub-binaries, which
> should make it faster.
>
> We hope to be able to add better optimization to a future version
> of the Erlang compiler, so that you will not have to write such
> contrived code to get the fastest possible bit syntax matching.
>
> /Bjorn
>
> Gaspar Chilingarov <> writes:
>
> > Hi all!
> >
> > I wish to share an experience which I had today when writing code.
> >
> [...]
> >
> > break_on_nl1(B) -> break_on_nl1(0, B).
> > break_on_nl1(Len, Bin) when Len == size(Bin) ->
> >       {Bin, <<>>};
> > break_on_nl1(Len, Bin) ->
> >       <<Msg:Len/binary, Symb, Tail/binary>> = Bin,
> >       case Symb of
> >               $\n -> {Msg, Tail};
> >               _ -> break_on_nl1(Len+1, Bin)
> >       end.
>
> --
> Björn Gustavsson, Erlang/OTP, Ericsson AB
>
> _______________________________________________
> erlang-questions mailing list
> 
> http://www.erlang.org/mailman/listinfo/erlang-questions
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20060927/210b9d64/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: cookbook.erl
Type: application/octet-stream
Size: 2133 bytes
Desc: not available
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20060927/210b9d64/attachment.obj>


More information about the erlang-questions mailing list