Solved

C#: Hashtable vs list vs dictionary

Posted on 2009-05-18
4
3,387 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Is Your DevOps Pipeline Leaking?

Is your CI/CD pipeline a hodge-podge of randomly connected tools? You’ve likely got a tool to fix one problem & then a different tool to fix another, resulting in a cluster of tools with overlapping functionality. Learn how to optimize your pipeline with Gartner's recommendations

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

752 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