Monday, July 7, 2008

.NET 3.5: StringCollection vs ArrayList

I've been running a couple of tests on the ArrayList and the StringCollection in .NET 3.5 to determine if the ArrayList still performs better than the StringCollection for insertions. There are a couple of articles on it, this one from 2004 being the one that also presents source code for a simple test that they've performed:http://weblogs.asp.net/justin_rogers/archive/2004/02/29/81680.aspx

Initially, I went with a normal-looking test but without creating the string table and that skewed the results. I initially wrote the code with the test on the StringCollection first and then the ArrayList and, according to the results, the ArrayList was a better performed. I then switched the positions of the ArrayList code and the StringCollection code and the results were reversed. Whichever executed first took longer. It was pretty normal for it to do that so...

I added the initial loading of strings into an array and then adding the array elements into the ArrayList and the StringCollection. Running another series of tests showed that the order still does matter - whichever executed first took less time.

The only way to prevent one test from affecting the other was to run the tests independently so I ran only the code for the ArrayList followed by the next run with only the code for the StringCollection. This time, I was able to get better results - the ArrayList consistently outperforms the StringCollection but the performance difference does seem quite marginal. Unless you're dealing with over 15 million strings, you're not going to take much of a performance hit so you can use whatever seems more convenient.