[erlang-questions] performance of re:replace/4 compared to plain code

Hynek Vychodil hynek@REDACTED
Mon May 10 21:04:36 CEST 2010


>From mine experience HiPE usually outperforms re module for this sort
of task, but I would write simply:

escape(Bin) -> << <<(case X of $.->$,; _->X end)>> || <<X>> <= Bin >>.

If performance really cares than you should use NIF but it is rarely
reason. Most time you need just fast enough and above code used to be.

And how benchmark? Just take real data sample. Duplicate to have
enough amount if necessary and then measure.

On Mon, May 10, 2010 at 7:45 PM, Pablo Platt <pablo.platt@REDACTED> wrote:
> Hi,
>
> I'm building a driver for a database that should be fast.
> I need to escape <<".">> from binary strings that goes to the database with <<",">>
>
> I can do this with the re:replace/4
> re:replace(<<"some.text.with.dots">>,<<"\\.">>,<<",">>,[{return,binary}]).
>
> I can also do it with a simple function:
> escape(Old) ->
>    escape(Old, <<>>).
>
> escape(<<>>, New) ->
>    New;
>
> escape_db(Old, New) ->
>    <<First:1/binary, Rest/binary>> = Old,
>    case First of
>        <<".">> ->
>            escape(Rest, <<New/binary, ",">>);
>        _ ->
>            escape(Rest, <<New/binary, First/binary>>)
>    end.
>
> The size of each binary string is <10 characters but there will be a lot of them processed all the time.
>
> Should I expect a difference in performance between the two ways?
> Which one should be faster and require less CPU?
> How can I benchmark it?
>
> Thanks
>
>
>
>



-- 
--Hynek (Pichi) Vychodil

Analyze your data in minutes. Share your insights instantly. Thrill
your boss.  Be a data hero!
Try GoodData now for free: www.gooddata.com


More information about the erlang-questions mailing list