Matcher performance improvements

Martin Buchholz martinrb at google.com
Thu Aug 13 01:30:28 UTC 2009


Hi regex/String team,

We would like to contribute a performance improvement for Matcher.

Please file a bug.

category: java_util_regex
Synopsis: Faster Matcher by replacing StringBuffer with StringBuilder

Description:
The old Matcher API and implementation depend on the old StringBuffer class.
The use of StringBuilder gives measurable performance improvement in
real world applications.
http://cr.openjdk.java.net/~martin/webrevs/openjdk7/Matcher-perf/
Written by Jeremy Manson.

Here's a microbenchmark, where it gives 25% win:

public class StringReplace {
  static String jabber = "Twas brillig, and the slithy toves " +
      "Did gyre and gimble in the wabe; " +
      "All mimsy were the borogoves, " +
      "And the mome raths outgrabe. " +
      " " +
      "Beware the Jabberwock, my son! " +
      "The jaws that bite, the claws that catch! " +
      "Beware the Jubjub bird, and shun " +
      "The frumious Bandersnatch!" +
      " " +
      "He took his vorpal sword in hand: " +
      "Long time the manxome foe he sought— " +
      "So rested he by the Tumtum tree, " +
      "And stood awhile in thought. " +
      " " +
      "And as in uffish thought he stood, " +
      "The Jabberwock, with eyes of flame, " +
      "Came whiffling through the tulgey wood, " +
      "And burbled as it came! " +
      " " +
      "One, two! One, two! and through and through " +
      "The vorpal blade went snicker-snack! " +
      "He left it dead, and with its head " +
      "He went galumphing back. " +
      " " +
      "And hast thou slain the Jabberwock? " +
      "Come to my arms, my beamish boy! " +
      "O frabjous day! Callooh! Callay!" +" " +
      "He chortled in his joy. " +
      " " +
      "'Twas brillig, and the slithy toves " +
      "Did gyre and gimble in the wabe; " +
      "All mimsy were the borogoves, " +
      "And the mome raths outgrabe?";

  public static void replace() {
    long start = System.currentTimeMillis();
    for (int i = 0; i < 100000; i++) {
      jabber.replaceAll("the", "ze");
    }
    long stop = System.currentTimeMillis();
    System.out.println(stop);
    System.out.println(start);

    System.out.println(stop - start);
  }

  public static void main(String[] args) {
    for (int i = 0; i < 5; i++) {
      replace();
    }
  }
}



More information about the core-libs-dev mailing list