FileDocCategorySizeDatePackage
RandomAccessTest.javaAPI DocExample2455Sun Oct 07 11:02:22 BST 2001None

RandomAccessTest

public class RandomAccessTest extends Object

Fields Summary
int
i
Constructors Summary
public RandomAccessTest(int i)

this.i = i;
Methods Summary
public static voidmain(java.lang.String[] args)

    int SIZE = args.length>0 ? Integer.parseInt(args[0]) : 100000;
    ArrayList arrayList = new ArrayList(SIZE);
    LinkedList linkedList = new LinkedList();
    RandomAccessTest r;
    for (int i = 0; i < SIZE; i++)
    {
      arrayList.add(r = new RandomAccessTest(i));
      linkedList.add(r);
    }

    testAll(arrayList, linkedList);
  
public static voidtestAll(java.util.List list1, java.util.List list2)

    testLoopCounter(list1);
    System.out.println("testLoopCounter(" + list2.getClass().getName() + ") not run as it takes too long");
    //testLoopCounter(list2);
    testIterator(list1);
    testIterator(list2);
    testIteratorWithLoopCounter(list1);
    testIteratorWithLoopCounter(list2);
    testRandomAccess(list1);
    testRandomAccess(list2);
  
public static longtestIterator(java.util.List list)

    long time = System.currentTimeMillis();
    int count = 0;
    for (Iterator i=list.iterator(); i.hasNext(); )
      count += ( (RandomAccessTest) i.next()).i;
    time = System.currentTimeMillis() - time;
    System.out.println(list.getClass().getName() + " in testIterator took "  + time + " millis to get result " + count);
    return time;
  
public static longtestIteratorWithLoopCounter(java.util.List list)

    long time = System.currentTimeMillis();
    int count = 0;
    Iterator i=list.iterator();
    for (int j=0, n=list.size() ; j < n ; j++)
      count += ( (RandomAccessTest) i.next()).i;
    time = System.currentTimeMillis() - time;
    System.out.println(list.getClass().getName() + " in testIteratorWithLoopCounter took "  + time + " millis to get result " + count);
    return time;
  
public static longtestLoopCounter(java.util.List list)

    long time = System.currentTimeMillis();
    int count = 0;
    for (int i=0, n=list.size(); i < n; i++)
      count += ( (RandomAccessTest) list.get(i)).i;
    time = System.currentTimeMillis() - time;
    System.out.println(list.getClass().getName() + " in testLoopCounter took "  + time + " millis to get result " + count);
    return time;
  
public static longtestRandomAccess(java.util.List list)

    if (list instanceof RandomAccess)
      return testLoopCounter(list);
    else
      return testIteratorWithLoopCounter(list);