Methods Summary |
---|
public static void | main(java.lang.String[] args)
//In order to isolate the effects of synchronization, we make sure
//that the garbage collector doesn't interfere with the test. So
//we use a bunch of pre-allocated, pre-sized collections, and
//populate those collections with pre-existing objects. No objects
//will be created or released during the timing phase of the tests.
int REPEAT = (args.length > 0) ? Integer.parseInt(args[0]) : 100;
//Vary the order.
testGeneralArray(REPEAT);
testIntArray(REPEAT);
testStringArray(REPEAT);
testIntArray(REPEAT);
testStringArray(REPEAT);
testGeneralArray(REPEAT);
testIntArray(REPEAT);
testGeneralArray(REPEAT);
testStringArray(REPEAT);
|
public static void | testGeneralArray(int REPEAT)
//initialize
Integer integer = new Integer(5);
ObjArrayList l = new ObjArrayList(CAPACITY);
for (int j = 0; j < CAPACITY; j++)
l.add(integer);
//Set an existing object
long time = System.currentTimeMillis();
for (int i = REPEAT; i > 0; i--)
for (int j = 0; j < CAPACITY; j++)
l.set(j, integer);
System.out.println("Setting an existing Integer(5) in object array took: " +
(System.currentTimeMillis()-time));
//Set an new object
time = System.currentTimeMillis();
for (int i = REPEAT/10; i > 0; i--)
for (int j = 0; j < CAPACITY; j++)
l.set(j, new Integer(j));
System.out.println("Setting a new Integer in object array took: " +
(System.currentTimeMillis()-time) + " x 10");
//Get an existing object without casting
Object o;
time = System.currentTimeMillis();
for (int i = REPEAT; i > 0; i--)
for (int j = 0; j < CAPACITY; j++)
o = l.get(j);
System.out.println("Getting an existing Object from object array took: " +
(System.currentTimeMillis()-time));
//Get an existing object
time = System.currentTimeMillis();
for (int i = REPEAT; i > 0; i--)
for (int j = 0; j < CAPACITY; j++)
integer = (Integer) l.get(j);
System.out.println("Getting an existing Integer(" + integer + ") from object array took: " +
(System.currentTimeMillis()-time));
//Get underlying int value
int num = 33;
time = System.currentTimeMillis();
for (int i = REPEAT; i > 0; i--)
for (int j = 0; j < CAPACITY; j++)
num = ((Integer) l.get(j)).intValue();
System.out.println("Getting an existing int (" + num + ") from object array took: " +
(System.currentTimeMillis()-time));
System.out.println();
|
public static void | testIntArray(int REPEAT)
//initialize
int integer = 5;
IntArrayList l = new IntArrayList(CAPACITY);
for (int j = 0; j < CAPACITY; j++)
l.add(integer);
//Set an existing int
long time = System.currentTimeMillis();
for (int i = REPEAT; i > 0; i--)
for (int j = 0; j < CAPACITY; j++)
l.set(j, integer);
System.out.println("Setting an existing int in int array took: " +
(System.currentTimeMillis()-time));
//Set an new int
time = System.currentTimeMillis();
for (int i = REPEAT; i > 0; i--)
for (int j = 0; j < CAPACITY; j++)
l.set(j, j);
System.out.println("Setting a new int in int array took: " +
(System.currentTimeMillis()-time));
//Get an existing int without casting
time = System.currentTimeMillis();
for (int i = REPEAT; i > 0; i--)
for (int j = 0; j < CAPACITY; j++)
integer = l.get(j);
System.out.println("Getting an existing int " + integer + " from int array took: " +
(System.currentTimeMillis()-time));
System.out.println();
|
public static void | testStringArray(int REPEAT)
//initialize
String string = "hello";
StringArrayList l = new StringArrayList(CAPACITY);
for (int j = 0; j < CAPACITY; j++)
l.add(string);
//Set an existing String
long time = System.currentTimeMillis();
for (int i = REPEAT; i > 0; i--)
for (int j = 0; j < CAPACITY; j++)
l.set(j, string);
System.out.println("Setting an existing String in String array took: " +
(System.currentTimeMillis()-time));
//Get an existing String without casting
time = System.currentTimeMillis();
for (int i = REPEAT; i > 0; i--)
for (int j = 0; j < CAPACITY; j++)
string = l.get(j);
System.out.println("Getting an existing String " + string + " from String array took: " +
(System.currentTimeMillis()-time));
System.out.println();
|