|
I did some tests myself, I just underestimated the speed of System.arraycopy()
In my problem the resulting collection would be almost empty. Bar.java private static final int REPS = 50; @SuppressWarnings("unchecked") public static void test(int n) throws InterruptedException { Collection a = new ArrayList(n); for(int i=0; i<n; i++) { a.add("bob"+i); } for (int r = 0; r < REPS; ++r) { ArrayList c = new ArrayList(a); long t1 = System.currentTimeMillis(); while( c.size() > 0 ){ c.remove(0); } long t2 = System.currentTimeMillis(); System.err.println("T" + n + ": " + (t2-t1)+ " ms"); } } In this testcase for n = 200000, System.arraycopy() is called 200000 times with an average of 100000 references to be moved. I should have done my profiling before... sorry. No worries - closing the issue as there's nothing major here to work on.
Henri Yandell made changes - 03/Nov/08 04:36 PM
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
For an input of 10,000, both were around 550 msec. For 100,000 the ArrayList was 58000, while the LinkedList was 84000. Hardly scientific as I'm not repeating the test in the same run so could be missing out on JIT improving a second run, not running multiple times etc. My suspicion is that the ArrayList constructor checks to see if things are ArrayLists and does quick arraycopies, while the LinkedList constructor just sits and plods along. I retested by changing the input to LinkedLists from ArrayLists and the time doubled up to 102000. Of course when I try LinkedList passing in to the LinkedList variant, it goes up to 125000. Ah well.
Point of all that - apart from implying that more testing is needed - is that the collection type used might want to depend on the type of the 'a' variable.