Link to home
Start Free TrialLog in
Avatar of Snapples
Snapples

asked on

C#: Hashtable vs list vs dictionary

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)?
Avatar of p_davis
p_davis

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?
Avatar of Snapples

ASKER

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

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.
ASKER CERTIFIED SOLUTION
Avatar of williamcampbell
williamcampbell
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial