Comparing collections - speed + flexibility!

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi there,

1)
I am looking for the best collections techniques to be used in my program.

Is hashtable or arraylist or any .net collection, which is the fastest when adding, getting out data???

2)
And also, just curious, Hashtable.Add (key, value), can i somehow make the hashtable to have this Add(key, value1, value2).

Thanks.
 
Question 1 will depend more on how you are indexing the data, do you need to
retrieve it by key or just by index? Both ways are very fast and in my
experience unlikely to be the bottleneck in your application. It would be
better to test them by profiling the performance of you application with
both (if they are interchangable) than just asking us about general
performance.

For Question 2, simply store a class that holds two values i.e.:
myHashtable.Add("key", new MyValue("val1", "val2"));
You could even subclass Hashtable and add your own overload of Add that had
that signature.

Richard

Chua Wen Ching said:
Hi there,

1)
I am looking for the best collections techniques to be used in my program.

Is hashtable or arraylist or any .net collection, which is the fastest
when adding, getting out data???
2)
And also, just curious, Hashtable.Add (key, value), can i somehow make the
hashtable to have this Add(key, value1, value2).
 
--
C#, .NET and Complex Adaptive Systems:
http://blogs.geekdojo.net/Richard
Chua Wen Ching said:
Hi Richard A. Lowe,

Thanks for the reply. Can you please check inlines?

1) profiling the performance of you application
--> Yeah i had used the clr profiler. Kind of confusing. Many diagrams and
confusing. Any good tips to learn it up. Thanks

I'm not expert in that app, I'm afraid. I usually just pick a section of
code and time it in my code, I'm afraid.
2) retrieve it by key or just by index?
--> Index by key. I had specific keys to check the tables. And are you
refering hashtable or any collections?

Great, you should use the Hashtable. Use the contstructor that has the int
and float parameters: the int is the initial capacity: the number of items
you think you will need. The loadfactor is a number that tells the
Hashtable how to balance speed vs. size. Set it to it's minimum value for
the greatest speed: 0.1f:

new Hashtable(numberOfItemsINeed, 0.1f);
3) What is your most frequent used collections?

Well, probably typed collections derived from CollectionBase
4) simply store a class that holds two values i.e.:
myHashtable.Add("key", new MyValue("val1", "val2"));
--> new MyValue, is this a constructor or a new class? Can you show some
snippets please? Thanks.

Yes, that's what it is - a custom class of yours. i.e.

class MyValue {
public MyValue(object key, object value) { // etc - make public fields
or properties here to give you the values }
}
5)
--> Oh you can overload the add. Hmm... i tried to override it. But i not
sure how? Do i need to inherit something then i can override it?

If you inherit from Hashtable you can add overloads, no need to override:

class MyValueHashtable : Hashtable
{
public void Add(object key, object value) { // etc... call the other Add
method, and construct a MyValue instance to store two values here }
}
Thanks a lot again. :)
 
2) retrieve it by key or just by index?
--> Index by key. I had specific keys to check the tables. And are you
refering hashtable or any collections?

If you are using a key instead of a simple numeric index, you can't use just
an arraylist (since it's just an array, no key). A listdictionary might be
fast for small collections; after that, use a HashTable. There's a
HybridDictionary class that will choose the best one for you.

-mike
MVP
 
Back
Top