Perl Tutorials - Herong's Tutorial Examples
Dr. Herong Yang, Version 5.00

Performance of Java substring() and indexOf()

This section provides a tutorial example to test performance of 2 Java built-in functions: substring() and indexOf() on a Window 2000 system.

To compare the performance with Java language, I wrote the following program, SubstringTest.java, in 2002 with almost identical statement structure as SubstringTest.pl, using substring() and indexOf():

/**
 * SubstringTest.java
 * Copyright (c) 2002 by Dr. Herong Yang, http://www.herongyang.com/
 */
import java.util.*;
class SubstringTest {
   static int numChar = 10;
   static int numTest = 1;
   static String baseString = null;
   static String subString = null;
   static Random randomGenerator = null;
   public static void main(String[] a) {
      if (a.length>0) numChar = Integer.parseInt(a[0]);
      if (a.length>1) numTest = Integer.parseInt(a[1]);
      randomGenerator = new Random();
      baseString = setString(numChar);
      subString = setString(numChar);
      long startTime = (new Date()).getTime();
      long numMatch = 0;
      for (int i=0; i<numTest; i++) {
         numMatch = test();
      }
      long endTime = (new Date()).getTime();
      long totalTime = (endTime - startTime)/1000;
      long averageTime = totalTime/numTest;
      System.out.println("Number of tests = "+numTest);
      System.out.println("Number of characters = "+numChar);
      System.out.println("Number of matches = "+numMatch);
      System.out.println("Total time = "+totalTime+" seconds");
      System.out.println("Average time = "+averageTime+" seconds");
   }
   private static String setString (int size) {
      StringBuffer str = new StringBuffer();
      for (int i=0; i<size; i++) {
         int n = randomGenerator.nextInt(96) + 32;
         char c = (char) n;
         str.append(c);
      }
      return str.toString();
   }
   private static long test() {
      int i,j,l;
      String str = null;
      int num = 0;
      int pos = -1;
      for (i=0; i<numChar; i++) {
         l = i+1;
         for (j=0; j<numChar-i; j++) {
            str = subString.substring(j,j+l);
            pos = baseString.indexOf(str);
            if (pos<0) num++;
         }
      }
      return num;
   }
}

Running it on the sample system, I got the following result:

>\j2sdk1.4.1_01\bin\java -cp . SubstringTest 50 1000
Number of tests = 1000
Number of characters = 50
Number of matches = 1261
Total time = 1 seconds
Average time = 0 seconds

>\j2sdk1.4.1_01\bin\java -cp . SubstringTest 100 1000
Number of tests = 1000
Number of characters = 100
Number of matches = 4986
Total time = 7 seconds
Average time = 0 seconds

>\j2sdk1.4.1_01\bin\java -cp . SubstringTest 200 1000
Number of tests = 1000
Number of characters = 200
Number of matches = 19933
Total time = 56 seconds
Average time = 0 seconds

Conclusion: Java is much more efficient than Perl with sub string and string match functions.

Sections in This Chapter

String Related Built-in Functions

Performance of Perl substr() and index()

Performance of Java substring() and indexOf()

Dr. Herong Yang, updated in 2008
Performance of Java substring() and indexOf()