我想对一个长度最多为30的字符串进行哈希计算。如果时间是我的主要考虑因素,应该采取什么样的最佳方案?该函数将被调用100多万次。目前我正在使用以下代码:
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;
}
Object.GetHashCode()
方法为什么不能使用?你好像要重新实现同样的概念。 - Ken Wayne VanderLindeuint
或使用0x7FFFFF
进行掩码处理。 - CodesInChaos