在C#中字符串的快速哈希函数 [英] A fast hash function for string in C#
本文介绍了在C#中字符串的快速哈希函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要凑长度的字符串高达30会是怎样做到这一点,如果时间是我关心的最好的主意。该函数将被调用超过100万次。目前我使用的foloowing code,
静态UINT64 CalculateHash(串读,布尔lowTolerance)
{
UINT64 HASHEDVALUE = 0;
INT I = 0;
而(I< read.Length)
{
HASHEDVALUE + = read.ElementAt(I)*(UINT64)Math.Pow(31,I);
如果(lowTolerance)I + = 2;
别的我++;
}
返回HASHEDVALUE;
}
解决方案
静态UINT64 CalculateHash(串读)
{
UINT64 HASHEDVALUE = 3074457345618258791ul;
的for(int i = 0; I< read.Length;我++)
{
HASHEDVALUE + =读[I]
HASHEDVALUE * = 3074457345618258799ul;
}
返回HASHEDVALUE;
}
这是一个高德纳哈希值。您也可以使用詹金斯。
I want to hash a string of length upto 30. What will be the best idea to do that if time is my concern. The function will be called over 100 million times. currently I am using the foloowing code,
static UInt64 CalculateHash(string read, bool lowTolerance)
{
UInt64 hashedValue = 0;
int i = 0;
while (i < read.Length)
{
hashedValue += read.ElementAt(i) * (UInt64)Math.Pow(31, i);
if (lowTolerance) i += 2;
else i++;
}
return hashedValue;
}
解决方案
static UInt64 CalculateHash(string read)
{
UInt64 hashedValue = 3074457345618258791ul;
for(int i=0; i<read.Length; i++)
{
hashedValue += read[i];
hashedValue *= 3074457345618258799ul;
}
return hashedValue;
}
This is a Knuth hash. You can also use Jenkins.
这篇关于在C#中字符串的快速哈希函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文