import java.util.*;
// @author Deborah A. Trytten
class Benchmark
{
private List myList; // The list to be measured
	
private Integer [] targets; // Some objects to put in the list
public static final int SIZE = 1000000; 
public Date start, end;
public long duration;
public Random myGen = new Random();
public Benchmark()
{
targets = new Integer[SIZE];  // an array of integers
for (int i=0; i<SIZE; i++)
   {
   targets[i] =  new Integer(  myGen.nextInt( SIZE ) );
   }
}

private void benchmarkSortForward()
{
myList.clear(); // empty out any previous data

for (int i=0; i< SIZE; i++)
   {
   myList.add(targets[i]);
   }
	
start = new Date(); // start the clock
Collections.sort( myList );
end = new Date(); // find the end time.
	
duration = end.getTime() - start.getTime();
System.out.println("Time for forward sort is " +  duration );
}

private void benchmarkSortReverse()
{
myList.clear(); // empty out any previous data
	
for (int i=0; i< SIZE; i++)
   {
	myList.add(targets[i]);
   }
	
start = new Date(); // start the clock
Collections.sort( myList, Collections.reverseOrder() );
end = new Date(); // find the end time.
	
duration = end.getTime() - start.getTime();
System.out.println("Time for reverse sort is " +  duration );
}

private void benchmarkAddAtEnd()
{
myList.clear(); // empty out any previous data
	
start = new Date(); // start the clock
for (int i=0; i< SIZE; i++)
   {
   myList.add(targets[i]);
   }
end = new Date(); // find the end time.

duration = end.getTime() - start.getTime();
System.out.println("Time for add at end is " +  duration );
}

public static void main(String[] args)
   {
   Benchmark bench = new Benchmark();
	   
   bench.myList = new ArrayList();
   System.out.println("For the ArrayList");
   bench.benchmarkAddAtEnd();
   bench.benchmarkSortForward();
   bench.benchmarkSortReverse();
   
   bench.myList = new LinkedList();
   System.out.println("\nFor the LinkedList");
   bench.benchmarkAddAtEnd();
   bench.benchmarkSortForward();
   bench.benchmarkSortReverse();
   }
}
