Java regionMatches Example and Performance

These Java examples use the regionMatches string method to test parts of strings.
RegionMatches. This method tests a region of one string against another. We provide a start index for both strings. We also provide a length argument (the number of chars to test on both).
With regionMatches, we can see if a substring patches another substring. We can avoid creating substrings. This saves allocations and improves performance.
An example. Let us begin with this example. We have a string literal with four concatenated words in it. We match each part of this literal.

Case: We can have regionMatches ignore the case of characters by passing a value of "true" as the first argument.

Java program that uses regionMatches public class Program { public static void main(String[] args) { String codes = "onetwothreeFour"; // Check for "two" at index 3. // ... Length is 3 chars. if (codes.regionMatches(3, "two", 0, 3)) { System.out.println(true); } // Check for "three" at index 6. // ... Length is 5 chars. if (codes.regionMatches(6, "three", 0, 5)) { System.out.println(true); } // Check for "FOUR" from "IS_FOUR" at index 11. // ... Length is 4 chars. if (codes.regionMatches(true, 11, "IS_FOUR", 3, 4)) { System.out.println(true); } } } Output true true true
Benchmark. Here is a benchmark of regionMatches. I test regionMatches against two substring calls—those substrings are compared with equals().Equals

Result: RegionMatches is faster. In this benchmark regionMatches is over 10 times faster than the version that uses substring and equals.

Java program that benchmarks regionMatches public class Program { public static void main(String[] args) { String part1 = "catdogbird"; String part2 = "dogbirdcat"; // Test two versions. if (part1.regionMatches(3, part2, 0, 3)) { System.out.println("True 1"); } if (part1.substring(3, 6).equals(part2.substring(0, 3))) { System.out.println("True 2"); } int count = 0; long t1 = System.currentTimeMillis(); // Version 1: use regionMatches to compare two substrings. for (int i = 0; i < 10000000; i++) { if (part1.regionMatches(3, part2, 0, 3)) { count++; } } long t2 = System.currentTimeMillis(); // Version 2: use substring to compare two substrings. for (int i = 0; i < 10000000; i++) { if (part1.substring(3, 6).equals(part2.substring(0, 3))) { count++; } } long t3 = System.currentTimeMillis(); // ... Times. System.out.println(count); System.out.println(t2 - t1); System.out.println(t3 - t2); } } Output True 1 True 2 20000000 15 ms: regionMatches 172 ms: substring, equals
Notes, indexes. When using methods like regionMatches and substring(), using correct indexes and lengths is important. The final argument on regionMatches is a length.Substringlength
Notes, performance. Avoiding allocations is one of the best performance optimizations. With regionMatches, we can achieve a big performance win if we can reduce substring allocations.
A summary. The arguments to regionMatches can be hard to get right at first. But the performance and code size improvements with regionMatches makes it worth the effort.
© 2007-2019 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.
HomeSearch
Home
Dot Net Perls