Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Key performance for a dictionary

Tags:

Is a string key faster than a int key in a Dictionary<,>?

like image 931
Mark T Avatar asked Apr 21 '11 11:04

Mark T


People also ask

What is a KPI dictionary?

Meaning of KPI in Englishabbreviation for key performance indicator: a way of measuring a company's progress towards the goals it is trying to achieve: A KPI is a performance metric for a specific business activity.

What is meant by key performance?

KPI stands for key performance indicator, a quantifiable measure of performance over time for a specific objective. KPIs provide targets for teams to shoot for, milestones to gauge progress, and insights that help people across the organization make better decisions.

What is key performance improvement?

Key Takeaways Key performance indicators (KPIs) measure a company's success versus a set of targets, objectives, or industry peers. KPIs can be financial, including net profit (or the bottom line, gross profit margin), revenues minus certain expenses, or the current ratio (liquidity and cash availability).


2 Answers

No. First of all, Dictionary [UPDATED] uses hash code of the keys to find them in its internal storage - rather than the keys. And Hashcode is an int. For int, it is just the value of the int, for string it has to be generated.

So using int is slightly faster.


In fact generating hash code for a string is a pretty complex process (snippet using Reflector) [Hope this is not taken as copyright breach because it is NOT]:

fixed (char* str = ((char*) this)) {     char* chPtr = str;     int num = 0x15051505;     int num2 = num;     int* numPtr = (int*) chPtr;     for (int i = this.Length; i > 0; i -= 4)     {         num = (((num << 5) + num) + (num >> 0x1b)) ^ numPtr[0];         if (i <= 2)         {             break;         }         num2 = (((num2 << 5) + num2) + (num2 >> 0x1b)) ^ numPtr[1];         numPtr += 2;     }     return (num + (num2 * 0x5d588b65)); } 
like image 55
Aliostad Avatar answered Sep 22 '22 05:09

Aliostad


I know this is pretty old and answered question, so this answer for everybody who look for it in future. Also for me it was interesting question and I tried to find practical answer to this question, and result is pretty interesting.

Generally String as key is much slower than int as key.

Code:

class Program {     object obj = new object();      Dictionary<long, object> longDict = new Dictionary<long, object>();     Dictionary<string, object> stringDict = new Dictionary<string, object>();      public static void Main(string[] args)     {         Program hash = new Program();          hash.Test(1000);         hash.Test(10000);         hash.Test(100000);         hash.Test(1000000);         hash.Test(10000000);          Console.Read();     }      private void Test(int iterations)     {         Console.WriteLine(String.Format("Test for {0} iterations", iterations));          longDict.Clear();         stringDict.Clear();          for (int i = 0; i < iterations; i++)         {             longDict.Add(i, obj);         }          for (int i = 0; i < iterations; i++)         {             stringDict.Add(i.ToString(), obj);         }          IntTest(iterations);         StringTest(iterations);     }      private void IntTest(int iteraions)     {         System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();         stopwatch.Start();          object test;          for (int i = 0; i < iteraions; i++) {             test = longDict[i];         }          stopwatch.Stop();         Console.WriteLine("Time elapsed: {0}", stopwatch.Elapsed);     }      private void StringTest(int iteraions)     {         System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();         stopwatch.Start();          object test;          for (int i = 0; i < iteraions; i++)         {             test = stringDict[i.ToString()];         }          stopwatch.Stop();         Console.WriteLine("Time elapsed: {0}", stopwatch.Elapsed);     } } 

Result:

enter image description here

like image 35
megabobik Avatar answered Sep 21 '22 05:09

megabobik