First, ArrayList and Hashtable are two very different types of
collections. You shouldn't be choosing between the two in terms of
performance but rather functionality. Do you need to keep a list of
items which you'll refer to by index and just loop through or do you
need a dictionary of items so you can look up some value by a custom
key? The answer to that will determine if you an ArrayList or
Hashtable (or a variant of each).
Also there's a big difference between complex functionality and
complex data storage (memory usage).
ArrayList may be more complicated than a simple array and it provides
a lot more functionality, but the memory overhead is only two
additional ints and two object references (extra object reference for
the array itself, extra object for the syncroot, an int for the size,
and an int for the internal version). So the extra memory overhead is
very minor compared to the actual contents of an array (normally at
least). If you're ever going to be adding/removing elements from the
array, then definitely want to use arraylist.
However, if once the array items are created the array is static, then
you can use ToArray to convert it to a standard array and store a
regular typed array. We use this approach a lot with public
properties and return variables--we don't want to return ArrayLists
because they don't provide type, so we use ArrayLists internal to a
method to generate a list and then convert to an ArrayList (which has
a copy overhead of course).
Hashtable is a lot more complicated in both functionality and data
storage. It uses 4 int variables and six object references. If each
Hashtable only stores a small number of items, it's a lot of overhead.
If each hashtable has many items, then the overhead is meaninglist.
If you truely have a lot of dictionaries with only a few items in
each, then look at ListDictionary. It uses only two ints and two
object references for it's data storage and provides a very fast
implementation for a dictionary as long as the data set is small. We
have a calculation engine that performs calculations on a dynamic
hierarchy of data for reporting. We typically load about 300,000
ListDictionary instances with 3-5 objects and the whole process of
loading the data from a DataReader into the dictionaries and then
running the required calculations is under 30ms (compared to 1-2
seconds for running the sql and 10-40 seconds for the 3rd party
reporting engine to generate the reports).
HTH,
Sam
------------------------------------------------------------
We're hiring! B-Line Medical is seeking Mid/Sr. .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.
On Fri, 27 Oct 2006 08:31:01 -0700, chrisben
<ch******@discussions.microsoft.comwrote:
>
Here is my problem Marc.
My list is small, but I may have many objects (hundreds), and each one will
hold a list like this. Hastable and ArrayList are complex data structure (I
think), which will require more resource regardless how small your list is.
That is why I start to think about string or stringbuffer, which I assume
less in term of resource and little or no difference interm of performance on
manipulation. For a short list, I am wondering whether it will be an overkill
to use ArrayList or Hashtable, especially that I may have hundreds of the
objects which hold this list structure.
What do you think?
Thanks
Chris