Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Character.isLetter() is much faster than string.matches(), because string.matches() compiles a new Pattern every time. Even caching the pattern, I think isLetter() would still beat it.</p> <hr> <p><strong>EDIT:</strong> Just ran across this again and thought I'd try to come up with some actual numbers. Here's my attempt at a benchmark, checking all three methods (<code>matches()</code> with and without caching the <code>Pattern</code>, and <code>Character.isLetter()</code>). I also made sure that there were both valid and invalid characters checked, so as not to skew things.</p> <pre><code>import java.util.regex.*; class TestLetter { private static final Pattern ONE_CHAR_PATTERN = Pattern.compile("\\p{L}"); private static final int NUM_TESTS = 10000000; public static void main(String[] args) { long start = System.nanoTime(); int counter = 0; for (int i = 0; i &lt; NUM_TESTS; i++) { if (testMatches(Character.toString((char) (i % 128)))) counter++; } System.out.println(NUM_TESTS + " tests of Pattern.matches() took " + (System.nanoTime()-start) + " ns."); System.out.println("There were " + counter + "/" + NUM_TESTS + " valid characters"); /*********************************/ start = System.nanoTime(); counter = 0; for (int i = 0; i &lt; NUM_TESTS; i++) { if (testCharacter(Character.toString((char) (i % 128)))) counter++; } System.out.println(NUM_TESTS + " tests of isLetter() took " + (System.nanoTime()-start) + " ns."); System.out.println("There were " + counter + "/" + NUM_TESTS + " valid characters"); /*********************************/ start = System.nanoTime(); counter = 0; for (int i = 0; i &lt; NUM_TESTS; i++) { if (testMatchesNoCache(Character.toString((char) (i % 128)))) counter++; } System.out.println(NUM_TESTS + " tests of String.matches() took " + (System.nanoTime()-start) + " ns."); System.out.println("There were " + counter + "/" + NUM_TESTS + " valid characters"); } private static boolean testMatches(final String c) { return ONE_CHAR_PATTERN.matcher(c).matches(); } private static boolean testMatchesNoCache(final String c) { return c.matches("\\p{L}"); } private static boolean testCharacter(final String c) { return Character.isLetter(c.charAt(0)); } } </code></pre> <p>And my output:</p> <pre>10000000 tests of Pattern.matches() took 4325146672 ns. There were 4062500/10000000 valid characters 10000000 tests of isLetter() took 546031201 ns. There were 4062500/10000000 valid characters 10000000 tests of String.matches() took 11900205444 ns. There were 4062500/10000000 valid characters</pre> <p>So that's almost 8x better, even with a cached <code>Pattern</code>. (And uncached is nearly 3x worse than cached.)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload