Solved

C#: Hashtable vs list vs dictionary

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

Suggested Solutions

Title # Comments Views Activity
Get distinct values from excel or comm seperated file 4 32
Powershell File Sort 8 41
How to read text with RegEx... 2 26
Install IIS7.5 on Windows Sever 2012 R2 4 24
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…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

820 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