Alex Thomson
Alex Thomson

Reputation: 143

Optimization of Regex

I am using jprofiler to help me reduce the CPU usage. Here are my two scenarios:

Scenario 1:

//Global Variables
    static Pattern escapeSequences = Pattern.compile("(\\r|\\n|\\t)");
    static Pattern printableCharacters = Pattern.compile("[^\\p{Print}]");

     String message = String.format(format, args);


          return printableCharacters.matcher(escapeSequences.matcher(message).replaceAll("")).replaceAll("?");

Scenario 2:

            for (int i=0; i<args.length; ++i) {
                if (args[i] instanceof String) {
                    args[i].replaceAll("(\\r|\\n|\\t)","").replaceAll("[^\\p{Print}]", "?");
                }
            }

return formater = String.format(format, args);

Scenario 1 runs in approximately 9% while scenario 2 runs in approximately 7%. What i do not understand here is that if the regex is pre-compiled once as opposed to every time, why is the cpu higher? What can I do to reduce the CPU below 7%.

Upvotes: 0

Views: 50

Answers (1)

endriu_l
endriu_l

Reputation: 1659

I believe that the following line: String.format(format, args); is the reason.

In the first example, You execute it prior to replacing characters, in second, You do it post. Depending on format variable, the processed string might get a lot bigger in first example, affecting the execution time.

Moreover, in second example the loop is not doing anything useful - replaceAll creates new String reference, and You are not assigning it anywhere. The compiler might just optimize this section and throw this loop away. Bottom line is that the result is different from the one returned by first example.

Upvotes: 1

Related Questions