This class is a helper class to quickly generate a strong hash code for a GUID type.
A strong hash function is important in Dictionary Lookups. If the hash is sufficently strong, a mod operation over the largest prime can be avoided, thus allowing for faster customized dictionary lookups. Method is the same as taking the last 4 bytes of a SHA1 hashsum, except zero is reserved and converted to 1.
Exemplo n.º 1
0
        public void Test()
        {
            for (int x = 0; x < 10000000; x++)
            {
                Guid value = Guid.NewGuid();

                int hash1 = GetHash(value);
                int hash2 = GuidSHA1Helper.ComputeHash(value);

                if (hash1 != hash2)
                {
                    throw new Exception();
                }
            }
        }
Exemplo n.º 2
0
        public void ComputeCustom()
        {
            Guid value = Guid.NewGuid();

            Stopwatch sw = new Stopwatch();

            sw.Reset();
            sw.Start();
            for (int x = 0; x < 10000000; x++)
            {
                _ = GuidSHA1Helper.ComputeHash(value);
            }
            sw.Stop();

            sw.Reset();
            sw.Start();
            for (int x = 0; x < 10000000; x++)
            {
                _ = GuidSHA1Helper.ComputeHash(value);
            }
            sw.Stop();

            Console.WriteLine(10000000 / sw.Elapsed.TotalSeconds / 1000000);
        }