Which Collection class to use

  • Thread starter Thread starter Scott Meddows
  • Start date Start date
S

Scott Meddows

I'm writing an NT service to provide security information enterprise wide. I would like to have all of this information cached
memory if a previous request loaded it into memory. Then using a common interface I would be able to invalidate and unload old
information as it is updated (Removing the item from the collection?)

My big question is what collection class will give me the greatest flexibility and greatest performance?

I hope this is clear... I really don't know how to describe what I'm trying to say.
 
IMHO, a hashtable would be a good choice. I'm not sure what kind of
information you are planning to store but I assume it would be some object
most likely with some unique identifier. You can add the information to the
hashtable as:

dim oHash as New Hashtable
o.Add(ID, mySecurityObject)

Then, when you need to access it again (either because you want to update
the information or remove the stale information) you can do something like:

if o.Contains(ID) then
'either this...
o.Remove(ID)
'or this..
o.Item(ID) = myUpdatedSecurityObject
end if

The Contains method lets you search for an existing object in the hashtable
structure extremely fast. The only requirement is that all the keys (IDs in
this case) must be unique.

I hope that gives you a good enough idea to proceed..
Imran.


Scott Meddows said:
I'm writing an NT service to provide security information enterprise wide.
I would like to have all of this information cached
memory if a previous request loaded it into memory. Then using a common
interface I would be able to invalidate and unload old
information as it is updated (Removing the item from the collection?)

My big question is what collection class will give me the greatest
flexibility and greatest performance?
 
Yeah, that helps. What kind of memory requirements are we talking about with this, though?

I will basically be keeping an object hierarchy in memory
 
Also, would I just inherit from this class or create a new hashtable and just shove my objects in there?
 
I believe memory shouldn't be an issue; When you add an object to the
hashtable, what you are really adding is a reference to the object which is
a 4-byte integer. Whatever collection you use to store your objects, thats
how items are going to be added to the collection - reference to your
objects (atleast for reference types; for value types I believe boxing is
involved since hashtable, arraylist, etc take in an Object type for the add
methods - although I'm not absolutely sure about that). The only extra
memory you need for hashtables is the memory required for storing the keys.
Thus, I think you should still be fine in terms of memory consumption. The
advantage that a hashtable data structure provides is the quick retrieval of
the items stored. The downside is that a hashtable does not store the items
in the sequence you added them which means you can't be sure of the order of
the items. Also, it does not have any sorting mechanism.

hope that helps..
Imran.
 
You might wanna implement DictionaryBase if you want typed structure instead
of Object Based.

HTH
rawCoder
 
Scott,

The class with the most posibilities is in my opinion the datatabel, it is
not necessary to use that with a database you know. Talking about
performance with this is in my opinion talking about parts of nanoseconds.

Just my thought on the general part of your message. (You even can set that
in a dataset and than save it for tempory or saving use on disk)

Cor

Scott Meddows said:
I'm writing an NT service to provide security information enterprise wide.
I would like to have all of this information cached
memory if a previous request loaded it into memory. Then using a common
interface I would be able to invalidate and unload old
information as it is updated (Removing the item from the collection?)

My big question is what collection class will give me the greatest
flexibility and greatest performance?
 
Will it be as fast as the hashtable?

rawCoder said:
You might wanna implement DictionaryBase if you want typed structure instead
of Object Based.

HTH
rawCoder
 
Scott,

Almost every collection derives from IList or Icontainer so there is not so
much diverence, even when you make your own collection and you do it right,
you implement those.

What performance are you looking for? Adding to a simple arraylist
10.000.000 objects takes one second on an avarage modern computer?

Cor
 
I agree with Cor - indeed, performance in terms of what? Adding to the
collection, looping the collection, retrieving items from the collection,
etc etc. You need to define your metrics for performance. If you are talking
about adding your objects to the collections, I believe most of the
collections perform more or less the same. For retrieving items from a
collection, as a I mentioned in my earlier post Hashtable (or any other
class inherited from the DictionaryBase) perform the best simply because of
the underlying data structure used to store objects by this collection. As
rawCoder mentioned, if you want a strongly typed collection, you can inherit
from the DictionaryBase class and it should perform equally well.

hope that helps...
Imran.
 
Thanks a lot for all your help, guys.

I think I'm going to try the hash table and the dictionary base and do some performance testing on each of those metrics.

Thanks
 
Hi Cor,

Sure DataTable is by far the most usable data container facilitating easiest
data retrieval.

But compared to just a little more than an arrayList ( Dictionary Classes ),
can there be any performance loss visible to naked eye of the user ( and I
am not talking about the nanoseconds here )

There should be some place where all these benchmarks are placed for
different data structures not jsut theoritical assumptions of slow insertion
fast retrieval.

Talking about DataStructures, some of them which are not available in .NET -
are they available via third party lib or something ( Tree being missed the
most ). I see some five to six parts article on MSDN about DataStructures
using the prime and the elite language of .NET .. Ms. C# ... Why the code is
not available in VB.NET ???

Thank You
rawCoder
 
rawCoder,

Because of your question, I did a little test, creating a datatable with
columns item, name, address, city was about 3 times slower than creating the
same thing with objects in an hashtable. (I created 1000 times a table from
1000 rows in both, at me the hastable was about 3 seconds while the
datatabel about 9). When I did a test with 10 times a table from 100000
records both times doubles.

The question was however greathest flexibility and performance.

Suppose I want to get a persons with a specific name and city. With the
datatable I have a lot of very fast inbuild methods as select,
datarrowcollection.find or dataview.

The fastest method with a hashtable is probably loop through it and you
should create that loop and when you than want a collection of doubles you
even has to do more.

When you want to make another object with methods in that, feel free,
however in my opinion you would always lost in flexibility against the
datatable, and I will come everytime with a problem that is to do with the
datatable just by adding a method or using an extra ADONET class.

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top