Java Performance Tuning

Java(TM) - see bottom of page

|home |services |training |newsletter |tuning tips |tool reports |articles |resources |about us |site map |contact us |

Our valued sponsors who help make this site possible
New Relic RPM - The Revolution in Java Performance Management is Here! 

ManageEngine: Monitor Java EE Transactions, App Servers, JMX, DB @ $795! 

The Commando Pattern code listing

JProfiler
Get rid of your performance problems and memory leaks!

JHawk - Java code metrics
All major metrics. Trial, Standalone, Eclipse and Command line versions.

FOR A LIMITED TIME!
TheServerSide Java Symposium 3/17-3/19 Las Vegas - $200 off: code JAVA2


Java Performance Training Courses
COURSES AVAILABLE NOW. We can provide training courses to handle all your Java performance needs

See Your Message Here
You could have your tool advertised here, to be seen by thousands of potential customers

Java Performance Tuning, 2nd ed
The classic and most comprehensive book on tuning Java

Java Performance Tuning Newsletter
Your source of Java performance news. Subscribe now!
Enter email:


New Relic
New Relic RPM - The Revolution in Java Performance Management is Here!

Zoho Corp
ManageEngine: Monitor Java EE Transactions, App Servers, JMX, DB @ $795!


JProfiler
Get rid of your performance problems and memory leaks!

JHawk - Java code metrics
All major metrics. Trial, Standalone, Eclipse and Command line versions.

FOR A LIMITED TIME!
TheServerSide Java Symposium 3/17-3/19 Las Vegas - $200 off: code JAVA2


//


import java.util.*;
import java.lang.reflect.*;
//
//
public class Commando
{
//
  public static void main(String[] args)
    throws Exception
  {
    ArrayList collection = new ArrayList();
    initializeList(collection);
//
    fulltest(collection);
    fulltest(collection);
    fulltest(collection);
    fulltest(collection);
  }
//
  public static void fulltest(ArrayList collection)
    throws Exception
  {
    test0(collection);
    test1(collection);
    System.out.println();
  }
//
  static int REPEAT = 100;
  public static int test0(ArrayList collection)
  {
    long start = System.currentTimeMillis();
    int count = 0;
    for (int j = REPEAT; j > 0; j--)
      for(int i = 0; i < collection.size(); i++)
      {
        if (((String) collection.get(i)).indexOf("xy") != -1 )
          count++;
      }
    System.out.println( (System.currentTimeMillis()-start) );
    return count;
  }
//
  public static int test1(ArrayList collection)
    throws Exception
  {
    long start = System.currentTimeMillis();
    Field elementDataField = ArrayList.class.getDeclaredField("elementData");
    elementDataField.setAccessible(true);
    int elementSize = collection.size();
    Object[] elementData = (Object[]) elementDataField.get(collection);
    int count = 0;
    for (int j = REPEAT; j > 0; j--)
      for(int i = 0; i < elementSize; i++)
      {
        if (((String) elementData[i]).indexOf("xy") != -1 )
          count++;
      }
    System.out.println( (System.currentTimeMillis()-start) );
    return count;
  }
//
  public static void initializeList(List collection)
  {
    System.out.println("Initializing...");
    //All four character strings. Extend to more characters
    //depending on your processor speed and memory availability
    char c = 'a';
    char d = 'a';
    char e = 'a';
    char f = 'a';
    StringBuffer sb = new StringBuffer(4);
    for (int ic = 0; ic < 26; ic++)
    {
      sb.setLength(0);
      sb.append(c++);
      d = e = f = 'a';
      for (int id = 0; id < 26; id++)
      {
        sb.setLength(1);
        sb.append(d++);
        e = f = 'a';
        for (int ie = 0; ie < 26; ie++)
        {
          sb.setLength(2);
          sb.append(e++);
          f = 'a';
          for (int i = 0; i < 26; i++)
          {
            sb.setLength(3);
            sb.append(f++);
            collection.add(sb.toString());
          }
        }
      }
    }
    System.out.println("Finished initializing.");
  }
//
}
//


Last Updated: 2010-03-01
Copyright © 2000-2010 Fasterj.com. All Rights Reserved.
All trademarks and registered trademarks appearing on JavaPerformanceTuning.com are the property of their respective owners.
Java is a trademark or registered trademark of Sun Microsystems, Inc. in the United States and other countries. JavaPerformanceTuning.com is not connected to Sun Microsystems, Inc. and is not sponsored by Sun Microsystems, Inc.
URL: http://www.JavaPerformanceTuning.com/articles/commando2.shtml
RSS Feed: http://www.JavaPerformanceTuning.com/newsletters.rss
Trouble with this page? Please contact us