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 3281274
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:47:51+00:00 2026-05-17T19:47:51+00:00

I found that dictionary lookup could be very slow if compared to flat array

  • 0

I found that dictionary lookup could be very slow if compared to flat array access. Any idea why? I’m using Ants Profiler for performance testing. Here’s a sample function that reproduces the problem:

    private static void NodeDisplace()
    {
        var nodeDisplacement = new Dictionary<double, double[]>();

        var times = new List<double>();
        for (int i = 0; i < 6000; i++)
        {
            times.Add(i * 0.02);
        }
        foreach (var time in times)
        {
            nodeDisplacement.Add(time, new double[6]);
        }

        var five = 5;
        var six = 6;
        int modes = 10;
        var arrayList = new double[times.Count*6];
        for (int i = 0; i < modes; i++)
        {
            int k=0;
            foreach (var time in times)
            {
                for (int j = 0; j < 6; j++)
                {

                    var simpelCompute = five * six;  // 0.027 sec
                    nodeDisplacement[time][j] = simpelCompute;  //0.403 sec
                    arrayList[6*k+j] = simpelCompute;  //0.0278 sec
                }

                k++;
            }
        }
    }

Notice the relative magnitude between flat array access and dictionary access? Flat array is about 20 times faster than dictionary access ( 0.403/0.0278), after taking into account of the array index manipulation ( 6*k+j).

As weird as it sounds, but dictionary lookup is taking a major portion of my time, and I have to optimize it.

  • 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-05-17T19:47:52+00:00Added an answer on May 17, 2026 at 7:47 pm

    Yes, I’m not surprised. The point of dictionaries is that they’re used to look up arbitrary keys. Consider what has to happen for a single array dereference:

    • Check bounds
    • Multiply index by element size
    • Add index to pointer

    Very, very fast. Now for a dictionary lookup (very rough; depends on implementation):

    • Potentially check key for nullity
    • Take hash code of key
    • Find the right slot for that hash code (probably a “mod prime” operation)
    • Probably dereference an array element to find the information for that slot
    • Compare hash codes
    • If the hash codes match, compare for equality (and potentially go on to the next hash code match)

    If you’ve got “keys” which can very easily be used as array indexes instead (e.g. contiguous integers, or something which can easily be mapped to contiguous integers) then that will be very, very fast. That’s not the primary use case for hash tables. They’re good for situations which can’t easily be mapped that way – for example looking up by string, or by arbitrary double value (rather than doubles which are evenly spaced, and can thus be mapped to integers easily).

    I would say that your title is misleading – it’s not that dictionary lookup is slow, it’s that when arrays are a more suitable approach, they’re ludicrously fast.

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

Sidebar

Related Questions

I have found that my HTML is, to be honest, very clunky. Small, simple
Whilst refactoring some legacy C++ code I found that I could potentially remove some
I'm using a RichTextBox in WinForms 3.5 and I found that when I programmatically
We’ve found that the unit tests we’ve written for our C#/C++ code have really
I've found that on some occasions I can edit the source while debugging. Are
I have found that there is generally a singe type or namespace that takes
I've installed Ubuntu Server (8.04) into Parallels and found that the system time/clock ran
I've been parsing through some log files and I've found that some of the
After trying to setup my site for Google Webmaster Tools I found that my
I have looked on FaceBook Developer page and found that it's possible to create

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.