Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7949431
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T01:59:47+00:00 2026-06-04T01:59:47+00:00

I am writing an application that is primarily performance intensive. After writing a quick

  • 0

I am writing an application that is primarily performance intensive. After writing a quick benchmark today I have found that dictionary access time is significantly slower than array access time (up to a 100 times), though both are O(1). I tried finding similar results on the internet, but to no avail. Does this seem right? It makes sense to me as there is hashing involved for the dictionary, and not for the array.

I am now weighing up the pros and cons of the dictionary, and considering if I should attempt a faster implementation. In at least 3 instances of my application I loop through all my dictionaries (there are quite a few) with a foreach loop. So now to get to my question, does the foreach loop have to go through the “slow” (I speak relatively here) hashing process, or does it simply maintain an internal list that it can quickly iterate over? If this is the case, I’d be more likely to stick with the dictionary.

For some extra info, the application is a simple 2D game, and the dictionary is to store in game entities. There really are very many though. And yes, I already have a working solution. This is post-optimization, not pre.

Here is the benchmark I wrote. It may be very wrong, as I’m no expert in the field:

        public static int dict(Dictionary<key,int> dict, key k)
        {
            int i;
            //dict.TryGetValue(k,out i);
            i = dict[k];  //both these version yield the same results

            return i;
        }

        public static int arr(int[,] arr, key k)
        {
            int i;
            i = arr[k.x, k.y];

            return i;
        }

        public struct key
        {
            public int x, y;

            public key (int x, int y){
                this.x = x;
                this.y = y;
            }
        }

        public static void Main()
        {
            int dim = 256;
            Dictionary<key,int> dictVar = new Dictionary<key,int>(dim*dim*10);

            int[,] arrVar = new int[dim,dim];

            for (int i = 0; i < dim; ++i)
            {
                for (int j = 0; j < dim; ++j)
                {
                    arrVar[i, j] = i + j * i;
                    dictVar.Add(new key(i, j), i + i * j);
                }  
            }

            const int iterations = 1000000;
            key k = new key(200, 200);
            TestTime(dict, dictVar, k, iterations);
            TestTime(arr, arrVar, k, iterations);
        }

        public static void TestTime<T1, T2, TR>(Func<T1, T2, TR> action, T1 obj1,
                                                T2 obj2, int iterations)
        {
            Stopwatch stopwatch = Stopwatch.StartNew();
            for (int i = 0; i < iterations; ++i)
                action(obj1, obj2);
            Console.WriteLine(action.Method.Name + " took " + stopwatch.Elapsed);
        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T01:59:49+00:00Added an answer on June 4, 2026 at 1:59 am

    It maintains an internal list it loops over, but the list is of type Entry<TKey, TValue>[], so it has to construct a KeyValuePair each time. I would guess this is what is causing the slowdown.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a very high performance multitasking, near real-time C# application. This performance was
I'm writing an application that requires all users to access data on a central
I have just started writing a multilingual application for the very first time. I
I have an application that I am currently writing that will use Spring and
Im writing an application that supposed to send coordinates in an SMS, but I've
I'm writing an application that will create difficult passwords for the user. The user
I'm writing an application that interacts with other processes on a x64 Vista machine.
I'm writing an application that utilizes JavaScript timeouts and intervals to update the page.
I am writing an application that is requiring me to do a DNS lookup
I am writing an application that helps book exchange between users. I am using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.