Collections Performance Benchmarks
I’ve been having fun of late pitting some Collections implementations against each other and getting some pretty peculiar results coming back. Well, I guess the only thing peculiar about them is that Vector keeps out performing ArrayList, even though Vector is synchronized and ArrayList in theory should be faster. So, one way that ArrayList can lose is when the two aren’t sized correctly upon construction and have to grow to accommodate their datasets. ArrayList grows by 50% with each resize, while Vector doubles. Having to resize is an expensive operation and this explains quite a bit. But even when I remove the resizing ( by sizing correctly upon construction ) ArrayList still gets pwned by Vector? Here’s the results from a test with random 40-character Strings being built into a unique Collection where the Collections are sized
.correctly. Notice how Vector and LinkedList perform virtually exactly the same, but ArrayList is slower, and getting slower as the dataset size increases. Of course, HashSet is still the hands down winner and what you should use when working with Collections that don’t care about ordering. TreeSet is a close second, but bear in mind that TreeSet can perform poorly when your dataset have similar Strings.
So, now it’s time to dig a bit deeper into the entire ArrayList versus Vector thing. What if I build each of them up with integers from 1 to x before the test runs, and then as the test call contains() on each integer 1 to x? Is contains faster or slower for ArrayList?
So, it looks like the calls to contains() are slower for ArrayList. What about the calls to add() with a presized Collection?
Yet again, Vector is a little bit faster than ArrayList! I’ve looked through the Java source code and so help me I swear the y are doing the same effective work – but Vector is synchronized and SHOULD BE SLOWER!
So, now I need to branch out and do some testing on other JDK’s and other OS’s. 1.5 and Linux, here I come!


















Was there a followup to this, or is Vector really faster than the ArrayList?