Solved

C#: Hashtable vs list vs dictionary

Posted on 2009-05-18
4
3,381 Views
Last Modified: 2013-12-17
I'm programming a game in XNA/C# and I have several objects that need to be stored.
I'm currently using Hashtables for this. But whenever I need to get an object I have to search in the Hashtable for the key, 60 times per second.
While I'm drawing my stuff to the screen I get a performance drop once in a while. So I'm wondering, what's the fastest way to store and retrieve objects? A hashtable, an (array)list or a dictionary (never used a dictionary before)?
0
Comment
Question by:Snapples
  • 2
4 Comments
 
LVL 22

Expert Comment

by:p_davis
ID: 24416115
hashtables are probably the quickest. a dictionary is really just a hashtable.

how are you drawing? maybe your approach is what is causing the performance issue?
0
 
LVL 2

Author Comment

by:Snapples
ID: 24416186
The drawing isn't the problem. I'm calculating collisions on my 3D models every frame, when I turn that off I don't have the performance problem.
// In my main class
        public override void Update(GameTime gameTime)
        {
            if (bHasCollision)
            {
                foreach (string key in MyCollisionTree.Collection.Keys)
                {
                    MyCollisionTree.GetCollisionBox(key).Update(World);
                }
            }
        }
 
 
// said update on the object
        public void Update(Matrix worldmatrix)
        {
            mxWorldTransformMatrix = worldmatrix * mxBoneIndexMatrix;
            TransformVertices();
        }
 
// PointList and TransformedPointList are Vector3[24]
        private void TransformVertices()
        {
            // Transform the vertices with the bonetransform matrix
            Vector3.Transform(PointList, ref mxWorldTransformMatrix, TransformedPointList);
        }

Open in new window

0
 
LVL 22

Expert Comment

by:p_davis
ID: 24416343
what if you try to enumerate using the pair

foreach(DictionaryEntry entry in MyCollisionTree)
{
        entry.Value.Update(World);
}

//did this without editor - don't know if it is exactly right.
//might give you a little boost.
0
 
LVL 12

Accepted Solution

by:
williamcampbell earned 500 total points
ID: 24416908
A hastable is the lowest level type of Dictionary which is a key / value pair
ArrayLists automatically "box" the value i.e. make it an object, so avoid that

I would suggest changing your string key to a long for a speed boost you may be suffering from "boxing" and comparing strings is slower than longs.



0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now