[英]need a better implementation of this FNV hashing alogithm in java
我有這個,我想提高效率,如果您需要我創建的整個哈希集數據結構,可以添加它,但是我總體上在尋找這樣的東西,它需要任意數量的字符串並將其存儲在我的自定義實現中哈希集:
private int hash(String key)
{
int prime = 31;
int hash = 0;
for(int i = 0; i < key.length(); i++)
{
hash *= prime;
hash ^= key.charAt(i);
}
if (hash < 0)
hash *= -1;
return hash % array.length;
}
一些觀察:
以下代碼效率低下:
if (hash < 0) hash *= -1;
此代碼更好,因為(1)它不使用處理器乘法指令,這會占用大量資源,並且(2)它不會定期產生分支錯誤預測:
hash &= ~0x80000000;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.