[jdom-interest] About JDOM performance
Alex Rosen
arosen at silverstream.com
Tue May 22 15:32:50 PDT 2001
Those pitfalls were not completely unintentional - my point was that the
difference in speed is insignificant, given the fact that a lot of other stuff
(allocations, method calls, etc) is likely to be going on, besides just List
manipulation. However, I cleaned up the code to get rid of as much overhead as
I could, and I increased the loop counter. The new results do show ArrayList
being about 25% faster than LinkedList. But, average time for a list operation
was still well under a millionth of a second.
--Alex
D:\z\Test2>java ListTest linked
Testing LinkedList
Time for add: 461 ms
Time for add+remove: 681 ms
Time for add+remove 2: 641 ms
Time for access: 160 ms
Time for access 2: 260 ms
Time for total ***: 2203 ms
Time for add: 411 ms
Time for add+remove: 681 ms
Time for add+remove 2: 631 ms
Time for access: 140 ms
Time for access 2: 250 ms
Time for total ***: 2113 ms
Time for add: 421 ms
Time for add+remove: 681 ms
Time for add+remove 2: 621 ms
Time for access: 150 ms
Time for access 2: 240 ms
Time for total ***: 2113 ms
Time for add: 421 ms
Time for add+remove: 671 ms
Time for add+remove 2: 621 ms
Time for access: 150 ms
Time for access 2: 240 ms
Time for total ***: 2103 ms
D:\z\Test2>java ListTest array
Testing ArrayList
Time for add: 261 ms
Time for add+remove: 651 ms
Time for add+remove 2: 450 ms
Time for access: 200 ms
Time for access 2: 121 ms
Time for total ***: 1683 ms
Time for add: 240 ms
Time for add+remove: 641 ms
Time for add+remove 2: 441 ms
Time for access: 200 ms
Time for access 2: 120 ms
Time for total ***: 1642 ms
Time for add: 240 ms
Time for add+remove: 641 ms
Time for add+remove 2: 441 ms
Time for access: 190 ms
Time for access 2: 120 ms
Time for total ***: 1632 ms
Time for add: 251 ms
Time for add+remove: 641 ms
Time for add+remove 2: 450 ms
Time for access: 191 ms
Time for access 2: 120 ms
Time for total ***: 1653 ms
-------------- next part --------------
import java.util.*;
public class ListTest
{
public static void main(String[] args)
{
boolean linked = (args.length > 0 && args[0].equals("linked"));
if (linked)
System.out.println("Testing LinkedList");
else
System.out.println("Testing ArrayList");
test(linked);
test(linked);
test(linked);
test(linked);
}
private static void test(boolean linked)
{
Double[] doubles = { new Double(1.0), new Double(2.0), new Double(3.0) };
//// Test add
long start = System.currentTimeMillis();
List list = createList(linked);
for (int i = 0; i < 1000000; i++)
{
if ((i % 5) == 0)
list.clear();
list.add(doubles[i % 3]);
}
if (list.size() < 0) // Make sure HotSpot doesn't optimize away anything (just in case)
System.exit(1);
long inter = interval("add", start);
//// Test add and remove from front
list = createList(linked);
for (int i = 0; i < 1000000; i++)
{
if ((i % 5) == 0)
list.clear();
list.add(0, doubles[i % 3]);
if ((i % 2) == 0)
list.remove(0);
}
if (list.size() < 0) // Make sure HotSpot doesn't optimize away anything (just in case)
System.exit(1);
inter = interval("add+remove", inter);
//// Test add and remove from middle
list = createList(linked);
for (int i = 0; i < 1000000; i++)
{
if ((i % 5) == 0)
list.clear();
list.add(doubles[i % 3]);
if ((i % 2) == 0)
list.remove(i % list.size());
}
if (list.size() < 0) // Make sure HotSpot doesn't optimize away anything (just in case)
System.exit(1);
inter = interval("add+remove 2", inter);
//// Test access via Iterator
list.clear();
for (int i = 0; i < 10; i++)
list.add(doubles[i % 3]);
double d = 0.0;
for (int i = 0; i < 100000; i++) // Note: 10x fewer iterations, since we access all 10 values.
{
Iterator iter = list.iterator();
while(iter.hasNext())
{
Double d2 = (Double)iter.next();
d += d2.doubleValue();
}
}
if (d < 0.0) // Make sure HotSpot doesn't optimize away anything (just in case)
System.exit(1);
inter = interval("access", inter);
//// Test access via random access
list.clear();
for (int i = 0; i < 10; i++)
list.add(doubles[i % 3]);
d = 0.0;
for (int i = 0; i < 1000000; i++)
{
Double d2 = (Double)list.get(i % 10);
d += d2.doubleValue();
}
if (d < 0.0) // Make sure HotSpot doesn't optimize away anything (just in case)
System.exit(1);
inter = interval("access 2", inter);
interval("total ***", start);
System.out.println();
}
private static List createList(boolean linked)
{
if (linked)
return new LinkedList();
else
return new ArrayList();
}
private static long interval(String label, long start)
{
long end = System.currentTimeMillis();
long diff = end - start;
System.out.println("Time for " + label + ": " + diff + " ms");
return end;
}
}
More information about the jdom-interest
mailing list