Which is faster, regex or if compares - Java

I have two possibilities, do a regular expression or do an if comparison.

If compares

if (!(modoImpressao.equals("IMPRESSORA") || modoImpressao.equals("PDF") || modoImpressao.equals("AMBOS"))) 

Regular Expression Matching

 if (!Pattern.compile("(IMPRESSORA)|(PDF)|(AMBOS)",Pattern.DOTALL).matcher(modoImpressao).find()){ throw new EspdNeverStopParametroInvalidoException(TspdConstMessages.IMPRIMIR_PARAMETRO_MODOIMPRESSAO_INVALIDO,"TspdImprimirNFCe"); } 

which is faster?

+6
source share
2 answers

The first snippet will almost certainly be faster since it does not need to parse the regular expression and match against it. Another variant:

 if (Arrays.asList("IMPRESSORA", "PDF", "AMBOS").contains(modoImpressao) 

which should not differ much from your first fragment, but perhaps more readable and concise.

Regular expressions are great, but use them only when you need to. This situation definitely does not guarantee the use of regular expressions; everything you do is compared to literal strings.

Here's an old saying by Jamie Zawinski that sounds like this:

Some people, faced with a problem, think: "I know, I will use regular expressions." Now they have two problems.

+13
source

If you use the regular expression only once, its probability will be slower, especially if you use find , where you really mean matches , as in your question.

When you save a compiled Pattern and use it several times, it can be faster than several peers.

This, however, depends on the context. If all the String tags you are testing are literals, and matches are more likely than unsuccessful, String.equals will be much faster since it tests the loop if the instances are the same.


The quickest solution is to pre-select, based on the easily verifiable property of the String candidate, which is different for all String s, for example. a char at a specific position or length , and do one equals for the selected String . In your case, both, the first character and the length are appropriate. In this case, I prefer length, since you need to check the length in any case before accessing the character to protect against empty Strings :

Preferred option using length :

 nomatch: { switch(modoImpressao.length()) { case 3: if(modoImpressao.equals("PDF")) break nomatch; break; case 5: if(modoImpressao.equals("AMBOS")) break nomatch; break; case 10: if(modoImpressao.equals("IMPRESSORA")) break nomatch; break; } throw new EspdNeverStopParametroInvalidoException( TspdConstMessages.IMPRIMIR_PARAMETRO_MODOIMPRESSAO_INVALIDO, "TspdImprimirNFCe"); } // one of the three values matched 

Option using the first char :

 nomatch: { if(modoImpressao.length()>0) switch(modoImpressao.charAt(0)) { case 'P': if(modoImpressao.equals("PDF")) break nomatch; break; case 'A': if(modoImpressao.equals("AMBOS")) break nomatch; break; case 'I': if(modoImpressao.equals("IMPRESSORA")) break nomatch; break; } throw new EspdNeverStopParametroInvalidoException(TspdConstMessages.IMPRIMIR_PARAMETRO_MODOIMPRESSAO_INVALIDO,"TspdImprimirNFCe"); } // one of the three values matched 
+1
source

All Articles