Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // With reference to http://pastebin.com/f287a2609
- using System;
- using System.Collections.Generic;
- using System.Text;
- namespace HashSpeed
- {
- class Program
- {
- static void Main(string[] args)
- {
- // Fix #1 - You are filling a dictionary with 1000000 values -
- // you better set up expectation with initial capacity
- Dictionary<string, int> nDic = new Dictionary<string, int>(1000000);
- int i;
- int x;
- Random rnd = new Random();
- Int64 tTimer = DateTime.Now.Ticks;
- // Fix #2 - User StringBuilder rather than string concatenations
- StringBuilder sb = new StringBuilder(16);
- Char[] chrs = new Char[16];
- for (i = 1; i <= 1000000; i++)
- {
- // Generate a random UUID
- for (x = 1; x <= 16; x++)
- {
- // Fix #3 - No need to go via encoder, in C# int are cast-able to Char
- chrs[x - 1] = (Char)rnd.Next(0, 255);
- }
- // Fix #4 - Better join all 16 chars in a single call
- String pUUID = sb.Clear().Append(chrs).ToString();
- //Check if pUUID exists in the dictionary
- if (!nDic.ContainsKey(pUUID))
- {
- //If not, add it to the dictionary
- nDic.Add(pUUID, 0);
- }
- }
- tTimer = DateTime.Now.Ticks - tTimer; //Stop the clock!
- Console.WriteLine("Operation completed in " + ((double)tTimer / (double)TimeSpan.TicksPerSecond).ToString());
- Console.WriteLine(nDic.Count.ToString() + " keys in dictionary");
- //Keep the window open at the end of the test
- // Fix #5 - Doing a while(1); is not a good way to wait for the user to end the program...
- Console.ReadKey();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement