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.

Here are the results that I came up with (time measured in ticks):


5 million string, independent executions

SC - 3047, 3063, 3047, 3078
AL - 3000, 3016, 3000, 3016


5 million string, populating from string array (skewed results)

AL - 219, 203, 219, 219
SC - 422, 422, 422, 438

SC - 203, 219, 234, 218
AL - 469, 484, 438, 485


5 million strings, ignoring string table (skewed results)

SC - 3219, 3063, 3110, 3031
AL - 2937, 2828, 2828, 2828

AL - 3000, 3016, 2985, 2984
SC - 2844, 2844, 2859, 2844


EDIT: I took a closer look at the StringCollection class and discovered that it is just a wrapper over the ArrayList. For more, read:
http://knitinr.blogspot.com/2008/07/stringcollection-internals.html

No comments: