Solved

C#: Hashtable vs list vs dictionary

Posted on 2009-05-18
4
3,379 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Replace &lt; with < 14 56
Web Form VB.Net  import CSV 4 27
Write to file when application closes (by logoff) - VB.NET 1 21
C# Comparing String Arrays. 5 26
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
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…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

867 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

21 Experts available now in Live!

Get 1:1 Help Now