Save This Page
Home » slf4j-1.5.5 » org.apache » log4j » performance » [javadoc | source]
    1   //      Copyright 1996-1999, International Business Machines 
    2   //      Corporation. All Rights Reserved.
    3   
    4   package org.apache.log4j.performance;
    5   
    6   import java.util.Date;
    7   
    8   /**
    9      Measure difference in performance of string concatenation versus
   10      creating an anonymous string array.
   11   
   12      <p>You should be able to see that anonymous string array
   13      construction is significatnly faster than string concatenation. The
   14      difference increases proportionally with the length of the strings
   15      to be concatanated.
   16   
   17      @author Ceki G&uuml;lc&uuml;
   18    */
   19   public class ConcatVsArray {
   20   
   21   
   22     static
   23     void  Usage() {
   24       System.err.println("Usage: java org.apache.log4j.performance.ConcatVsArray " +
   25   		       "string1 string2 runLength\n" +
   26   		       "       where runLength is an integer.");
   27       System.exit(1);
   28     }
   29   
   30     public static void main(String args[]) {
   31   
   32       if(args.length != 3) {
   33         Usage();
   34       }    
   35   
   36       String s1 = args[0];
   37       String s2 = args[1];
   38       int runLength = 0;
   39       try {
   40         runLength = Integer.parseInt(args[2]);      
   41       }
   42       catch(java.lang.NumberFormatException e) {
   43         System.err.println(e);
   44         Usage();
   45       }      
   46   
   47       double micros;
   48   
   49       String[] sa;
   50       long before = new Date().getTime();
   51       for(int i = 0; i < runLength; i++) {
   52         sa = new String[]{s1, s2};
   53       }
   54       micros = (new Date().getTime() - before)*1000.0/runLength;
   55       System.out.println("The anonymous array loop took around " + micros + " microseconds.");
   56   
   57       String s;    
   58       before = new Date().getTime();
   59       for(int i = 0; i < runLength; i++) {
   60         s = s1 + s2;
   61       }
   62       micros = (new Date().getTime() - before)*1000.0/runLength;
   63       System.out.println("The append loop took around " + micros + " microseconds.");
   64   
   65     }
   66   }

Save This Page
Home » slf4j-1.5.5 » org.apache » log4j » performance » [javadoc | source]