Matt Gerrans
Posts: 1153
Nickname: matt
Registered: Feb, 2002
|
|
Re: using timers
|
Posted: Apr 11, 2003 6:06 PM
|
|
Here's an example:
import java.util.*;
public class TimeTest implements Runnable
{
private static Random random = new Random();
private List list;
private final int itemCount;
public TimeTest( List list, int itemCount )
{
this.list = list; // Should also assert(list != null);
this.itemCount = itemCount;
}
public void run()
{
for( int i = 0; i < itemCount; i++ )
list.add( new Integer( random.nextInt() ) );
// Fool with the list:
for( int i = 0; i < itemCount; i++ )
{
// Randomly choose to remove from the end or begining:
int where = random.nextInt(2) > 0 ? 0 : list.size()-1;
// Also randomly choose whether to add or remove an item:
if( random.nextInt(2) > 0 )
list.add(where, new Integer(where));
else
list.remove(where);
}
}
public static long timeThis( Runnable runner )
{
long start = System.currentTimeMillis();
runner.run();
return System.currentTimeMillis() - start;
}
public static void main( String [] args )
{
int itemCount = 50000;
for( int i = 0; i < 10; i++ )
{
long t = timeThis( new TimeTest( new ArrayList(), itemCount ) );
System.out.print( "Test " + (i+1) +": ArrayList took " + t + " milliseconds, " );
t = timeThis( new TimeTest( new LinkedList(), itemCount ) );
System.out.println( "LinkedList took " + t + " milliseconds." );
}
}
}
|
|