Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

C#: Hashtable vs list vs dictionary

Posted on 2009-05-18
4
Medium Priority
?
3,394 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 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

730 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