performance of re:replace/4 compared to plain code

Pablo Platt pablo.platt@REDACTED
Mon May 10 19:45:38 CEST 2010


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



      


More information about the erlang-questions mailing list