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

  • Home
  • SEARCH
  • 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 186219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T15:36:30+00:00 2026-05-11T15:36:30+00:00

When using a Guid as an index for a Dictionary , is it better

  • 0

When using a Guid as an index for a Dictionary, is it better to use the Guid object, or the string representation of the Guid?

I just refactored some code which was using string to use the object, because there were new Guid() calls all over the place. But that left me wondering what the performance issues might be. (The collections are fairly small, but they get iterated lots of times.)

  • 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. 2026-05-11T15:36:31+00:00Added an answer on May 11, 2026 at 3:36 pm

    The Guid should be quicker, as the comparison is simpler – just a few direct bytes. The string involves a dereference and lots more work.

    Of course – you could profile ;-p

    Evidence:

    Searching for 7f9b349f-f36f-94de-ad96-04279ddf6ecf As guid: 466; -1018643328 As string: 512; -1018643328 Searching for 870ba465-08f2-c872-cfc9-b3cc1ffa09de As guid: 470; 1047183104 As string: 589; 1047183104 Searching for d2376f8a-b8c9-4633-ee8e-9679bb30f918 As guid: 423; 1841649088 As string: 493; 1841649088 Searching for 599889e8-d5fd-3618-4c4f-cb620e6f81bb As guid: 488; -589561792 As string: 493; -589561792 Searching for fb64821e-c541-45f4-0fd6-1c772189dadf As guid: 450; 1389733504 As string: 511; 1389733504 Searching for 798b9fe5-ba15-2753-357a-7637161ee48a As guid: 415; 779298176 As string: 504; 779298176 Searching for 12ba292e-8e59-e5d0-7d04-e811a237dc21 As guid: 457; 558250944 As string: 564; 558250944 Searching for 05b3ce14-dfbf-4d3a-1503-ced515decb81 As guid: 413; 1658205056 As string: 504; 1658205056 Searching for 8db4a556-0a65-d8cb-4d0d-0104245d18b8 As guid: 415; 696231936 As string: 506; 696231936 Searching for c49cf80c-5537-fba5-eebd-8ad21bba09c4 As guid: 459; 2100976384 As string: 557; 2100976384 

    based on:

    using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; static class Program {      static void Main()     {         Random rand = new Random(123456);         int COUNT = 1000;         Dictionary<Guid, int> guids = new Dictionary<Guid, int>(COUNT);         Dictionary<string, int> strings = new Dictionary<string, int>(             COUNT, StringComparer.Ordinal);          byte[] buffer = new byte[16];         for (int i = 0; i < COUNT; i++)         {             rand.NextBytes(buffer);             Guid guid = new Guid(buffer);             int val = rand.Next();             guids.Add(guid, val);             strings.Add(guid.ToString(), val);         }          for(int i = 0 ; i < 10 ; i++) {             int index = rand.Next(COUNT);             Guid guid = guids.Keys.Skip(index).First();             Console.WriteLine('Searching for ' + guid);             int chk = 0;             const int LOOP = 5000000;             Stopwatch watch = Stopwatch.StartNew();             for (int j = 0; j < LOOP; j++)             {                 chk += guids[guid];             }             watch.Stop();             Console.WriteLine('As guid: ' + watch.ElapsedMilliseconds                    + '; ' + chk);             string key = guid.ToString();             chk = 0;             watch = Stopwatch.StartNew();             for (int j = 0; j < LOOP; j++)             {                 chk += strings[key];             }             watch.Stop();             Console.WriteLine('As string: ' + watch.ElapsedMilliseconds                    + '; ' + chk);         }         Console.ReadLine();      } } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 128k
  • Answers 128k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Yes, in short, it is linear. "What the compiler actually… May 12, 2026 at 5:47 am
  • Editorial Team
    Editorial Team added an answer logrotate is your friend May 12, 2026 at 5:47 am
  • Editorial Team
    Editorial Team added an answer The exception class "Exception" is declared in the unit SysUtils.… May 12, 2026 at 5:47 am

Related Questions

We're using SQL Server 2005 to track a fair amount of constantly incoming data
I'm new to MVC land, and have an app that i'm working on. i
I am actually finding that chkContactType.Items is empty when I step through the code.
I have an Oracle project that would be a good fit for using GUIDs

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.