471,089 Members | 1,294 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,089 software developers and data experts.

Comparing collections - speed + flexibility!

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.
--
Regards,
Chua Wen Ching :)
Nov 16 '05 #1
6 2150
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" <ch************@nospam.hotmail.com> wrote in message
news:A1**********************************@microsof t.com...
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.
--
Regards,
Chua Wen Ching :)

Nov 16 '05 #2


--
C#, .NET and Complex Adaptive Systems:
http://blogs.geekdojo.net/Richard
"Chua Wen Ching" <ch************@nospam.hotmail.com> wrote in message
news:28**********************************@microsof t.com...
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)
You could even subclass Hashtable and add your own overload of Add that had that signature. --> 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. :)
--
Regards,
Chua Wen Ching :)
"Richard A. Lowe" wrote:
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" <ch************@nospam.hotmail.com> wrote in message
news:A1**********************************@microsof t.com...
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.
--
Regards,
Chua Wen Ching :)


Nov 16 '05 #3
A good comprehensive read is Scott Mitchell's two part article,
starting here:
http://msdn.microsoft.com/library/de...ures_guide.asp

--
Scott
http://www.OdeToCode.com

On Tue, 6 Jul 2004 04:45:01 -0700, "Chua Wen Ching"
<ch************@nospam.hotmail.com> wrote:
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.


Nov 16 '05 #4
Hi Scott Allen,

Thanks for the link. :)
--
Regards,
Chua Wen Ching :)
"Scott Allen" wrote:
A good comprehensive read is Scott Mitchell's two part article,
starting here:
http://msdn.microsoft.com/library/de...ures_guide.asp

--
Scott
http://www.OdeToCode.com

On Tue, 6 Jul 2004 04:45:01 -0700, "Chua Wen Ching"
<ch************@nospam.hotmail.com> wrote:
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.


Nov 16 '05 #5
Hi Richard A. Lowe,

Thanks for the tips. Cool!

I will go for hashtable then.

Cheers,
--
Regards,
Chua Wen Ching :)
"Richard A. Lowe" wrote:


--
C#, .NET and Complex Adaptive Systems:
http://blogs.geekdojo.net/Richard
"Chua Wen Ching" <ch************@nospam.hotmail.com> wrote in message
news:28**********************************@microsof t.com...
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)
You could even subclass Hashtable and add your own overload of Add that had that signature.

--> 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. :)
--
Regards,
Chua Wen Ching :)
"Richard A. Lowe" wrote:
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" <ch************@nospam.hotmail.com> wrote in message
news:A1**********************************@microsof t.com...
> 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.
> --
> Regards,
> Chua Wen Ching :)


Nov 16 '05 #6
> 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
Nov 16 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Phil... | last post: by
6 posts views Thread by buildpc | last post: by
5 posts views Thread by Dale | last post: by
8 posts views Thread by mast2as | last post: by
4 posts views Thread by wang frank | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.