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 |
Tools: | GC log analysers| Multi-tenancy tools| Books| SizeOf| Thread analysers|

Our valued sponsors who help make this site possible
JProfiler: Get rid of your performance problems and memory leaks! 

Training online: Threading Essentials course 

The Commando Pattern code listing

JProfiler
Get rid of your performance problems and memory leaks!


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

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:


Training online
Threading Essentials course


JProfiler
Get rid of your performance problems and memory leaks!


//


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: 2018-05-28
Copyright © 2000-2018 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 Oracle Corporation in the United States and other countries. JavaPerformanceTuning.com is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
URL: http://www.JavaPerformanceTuning.com/articles/commando2.shtml
RSS Feed: http://www.JavaPerformanceTuning.com/newsletters.rss
Trouble with this page? Please contact us