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.

Table of Contents

 About This Book

 Perl on Linux Systems

 ActivePerl on Windows Systems

 Data Types: Values and Variables

 Expressions, Operations and Simple Statements

 User Defined Subroutines

 Perl Built-in Debugger

 Name Spaces and Perl Module Files

 Symbolic (or Soft) References

 Hard References - Addresses of Memory Objects

 Objects (or References) and Classes (or Packages)

 Typeglob and Importing Identifiers from Other Packages

String Built-in Functions and Performance

 String Related Built-in Functions

 Performance of Perl substr() and index()

Performance of Java substring() and indexOf()

 File Handles and Data Input/Output

 Open Files in Binary Mode

 Open Directories and Read File Names

 File System Functions and Operations

 Converting Perl Script to Executable Binary

 Using DBM Database Files

 Using MySQL Database Server

 Socket Communication Over the Internet

 XML::Simple Module - XML Parser and Generator

 XML Communication Model

 SOAP::Lite - SOAP Server-Client Communication Module

 Perl Programs as IIS Server CGI Scripts

 CGI (Common Gateway Interface)

 XML-RPC - Remote Procedure Call with XML and HTTP

 RPC::XML - Perl Implementation of XML-RPC

 Integrating Perl with Apache Web Server

 CGI.pm Module for Building Web Pages

 LWP::UserAgent and Web Site Testing

 References

 PDF Printing Version