1、RSHash
unsigned int RSHash(const std::string& str)
{
   unsigned int b    = 378551;
   unsigned int a    = 63689;
   unsigned int hash = 0;
 
   for(std::size_t i = 0; i < str.length(); i++)
   {
      hash = hash * a + str[i];
      a    = a * b;
   }
 
   return hash;
}
 
2、JSHash
unsigned int JSHash(const std::string& str)
{
   unsigned int hash = 1315423911;
   for(std::size_t i = 0; i < str.length(); i++)
   {
      hash ^= ((hash << 5) + str[i] + (hash >> 2));
   }
   return hash;
}
 
3、PJWHash
unsigned int PJWHash(const std::string& str)
{
   unsigned int BitsInUnsignedInt = (unsigned int)(sizeof(unsigned int) * 8);
   unsigned int ThreeQuarters     = (unsigned int)((BitsInUnsignedInt  * 3) / 4);
   unsigned int OneEighth         = (unsigned int)(BitsInUnsignedInt / 8);
   unsigned int HighBits          = (unsigned int)(0xFFFFFFFF) << (BitsInUnsignedInt - OneEighth);
   unsigned int hash              = 0;
   unsigned int test              = 0;
 
   for(std::size_t i = 0; i < str.length(); i++)
   {
      hash = (hash << OneEighth) + str[i];
 
      if((test = hash & HighBits)  != 0)
      {
         hash = (( hash ^ (test >> ThreeQuarters)) & (~HighBits));
      }
   }
   return hash;
}
 
4、ELFHash
unsigned int ELFHash(const std::string& str)
{
   unsigned int hash = 0;
   unsigned int x    = 0;
 
   for(std::size_t i = 0; i < str.length(); i++)
   {
      hash = (hash << 4) + str[i];
      if((x = hash & 0xF0000000L) != 0)
      {
         hash ^= (x >> 24);
      }
      hash &= ~x;
   }
 
   return hash;
}
 
5、BKDRHash
unsigned int BKDRHash(const std::string& str)
{
   unsigned int seed = 131; // 31 131 1313 13131 131313 etc..
   unsigned int hash = 0;
 
   for(std::size_t i = 0; i < str.length(); i++)
   {
      hash = (hash * seed) + str[i];
   }
   return hash;
}
 
6、SDBMHash
unsigned int SDBMHash(const std::string& str)
{
   unsigned int hash = 0;
 
   for(std::size_t i = 0; i < str.length(); i++)
   {
      hash = str[i] + (hash << 6) + (hash << 16) - hash;
   }
 
   return hash;
}
 
7、DJBHash(times33)-这个用得非常多,很多库都用它。
unsigned int DJBHash(const std::string& str)
{
   unsigned int hash = 5381;
 
   for(std::size_t i = 0; i < str.length(); i++)
   {
      hash = ((hash << 5) + hash) + str[i];
   }
   return hash;
}
 
8、DEKHash
unsigned int DEKHash(const std::string& str)
{
   unsigned int hash = static_cast(str.length());
 
   for(std::size_t i = 0; i < str.length(); i++)
   {
      hash = ((hash << 5) ^ (hash >> 27)) ^ str[i];
   }
 
   return hash;
}
 
9、BPHash
unsigned int BPHash(const std::string& str)
{
   unsigned int hash = 0;
   for(std::size_t i = 0; i < str.length(); i++)
   {
      hash = hash << 7 ^ str[i];
   }
 
   return hash;
}
 
10、FNVHash
unsigned int FNVHash(const std::string& str)
{
   const unsigned int fnv_prime = 0x811C9DC5;
   unsigned int hash = 0;
   for(std::size_t i = 0; i < str.length(); i++)
   {
      hash *= fnv_prime;
      hash ^= str[i];
   }
 
   return hash;
}
 
11、APHash
unsigned int APHash(const std::string& str)
{
   unsigned int hash = 0xAAAAAAAA;
 
   for(std::size_t i = 0; i < str.length(); i++)
   {
      hash ^= ((i & 1) == 0) ? (  (hash <<  7) ^ str[i] * (hash >> 3)) :
                               (~((hash << 11) + (str[i] ^ (hash >> 5))));
   }
 
   return hash;
}
 
12、MurmurHash - 非常新的一个哈希算法,应该是目前效率最高的一个哈希算法,使用率很高。(伪代码来自维基百科)
Murmur2(key, len, seed)
    m <- 0x5bd1e995
    r <- 24
    seed  0x9747b28c
    hash <- seed XOR len
    for each fourByteChunk of key
        k <- fourByteChunk
        k <- k * m
        k <- k XOR (k >> r)
        k <- k * m
 
        hash <- hash * m
        hash <- hash XOR k
 
    with any remainingBytesInKey
        remainingBytes  SwapEndianOrderOf(remainingBytesInKey)
 
        hash <- hash XOR remainingBytes
        hash <- hash * m
 
    hash <- hash XOR (hash >> 13)
    hash <- hash * m
    hash <- hash XOR (hash >> 15)
 
 
 
MurmurHash算法:高运算性能,低碰撞率,由Austin Appleby创建于2008年,现已应用到Hadoop、libstdc++、nginx、libmemcached等开源系统。2011年Appleby被Google雇佣,随后Google推出其变种的CityHash算法。

官方网站:https://sites.google.com/site/murmurhash/

MurmurHash算法,自称超级快的hash算法,是FNV的4-5倍。官方数据如下:

OneAtATime – 354.163715 mb/sec 
FNV – 443.668038 mb/sec 
SuperFastHash – 985.335173 mb/sec 
lookup3 – 988.080652 mb/sec 
MurmurHash 1.0 – 1363.293480 mb/sec 
MurmurHash 2.0 – 2056.885653 mb/sec

但也有文章声称,只有当key的长度大于10字节的时候,MurmurHash的运算速度才快于DJB。“从计算速度上来看,MurmurHash只适用于已知长度的、长度比较长的字符”。

 
 

常用的Hash算法的更多相关文章

  1. 一致性hash算法详解

    转载请说明出处:http://blog.csdn.net/cywosp/article/details/23397179     一致性哈希算法在1997年由麻省理工学院提出的一种分布式哈希(DHT) ...

  2. 【区块链】【一】Hash 算法【转】

    问题导读1.哈希算法在区块链的作用是什么?2.什么是哈希算法?3.哈希算法是否可逆?4.比特币采用的是什么哈希算法? 作用在学习哈希算法前,我们需要知道哈希在区块链的作用哈希算法的作用如下:区块链通过 ...

  3. 一致性hash算法及java实现

    一致性hash算法是分布式中一个常用且好用的分片算法.或者数据库分库分表算法.现在的互联网服务架构中,为避免单点故障.提升处理效率.横向扩展等原因,分布式系统已经成为了居家旅行必备的部署模式,所以也产 ...

  4. 一致性Hash算法说明

    本文章比较好的说明了一致性Hash算法的概念 Hash算法一般分为除模求余和一致性Hash1.除模求余:当新增.删除机器时会导致大量key的移动2.一致性Hash:当新增.删除机器时只会影响到附近的k ...

  5. 【策略】一致性Hash算法

    转载请说明出处:http://blog.csdn.net/cywosp/article/details/23397179     一致性哈希算法在1997年由麻省理工学院提出的一种分布式哈希(DHT) ...

  6. HASH算法小结

    一.简述 HASH算法的本质是特征提取——将某种不太好表示的特征,通过某种压缩的方式映射成一个值.这样,就可以优雅解决一部分难以解决的特征统计问题. 同时考虑到hash算法的本质是个概率算法,因此并不 ...

  7. 一致性hash 算法 (转)

    转载请说明出处:http://blog.csdn.net/cywosp/article/details/23397179     一致性哈希算法在1997年由麻省理工学院提出的一种分布式哈希(DHT) ...

  8. 《算法 - 一致性 (hash) 算法》

    图片摘自: 每天进步一点点——五分钟理解一致性哈希算法(consistent hashing) 一:背景 - 一致性哈希算法在1997年由麻省理工学院的Karger等人在解决分布式Cache中提出的. ...

  9. 一致性Hash算法(转)

    一致性Hash算法提出了在动态变化的Cache环境中,判定哈希算法好坏的四个定义: 1.平衡性(Balance):平衡性是指哈希的结果能够尽可能分布在所有的缓冲(Cache)中去,这样可以使得所有的缓 ...

随机推荐

  1. Linux CC攻击脚本

    CC(ChallengeCollapsar)主要是用来攻击页面的.大家都有这样的经历,就是在访问论坛时,如果这个论坛比较大,访问的人比较多,打开页面的速度会比较慢,访问的人越多,论坛的页面越多,数据库 ...

  2. Eclipse导出可执行Jar文件(包含第三方Jar包)

    1. 首先,右键你的Java工程,选择Export,在Java文件夹下选择Runnable JAR file,如下图所示: 2. 选择Runnable JAR file后,会弹出如下所示的对话框,选择 ...

  3. 搜索功能demo

    代码如下: <html> <head> <meta http-equiv="X-UA-Compatible" content="IE=edg ...

  4. No suitable driver found for jdbc:mysql://localhost/dbname

    把mysql-connector-java的jar包放入jdk/jre/lib/ext文件下

  5. C# 正则表达式 验证:数字、带小数点数字、电话和手机

    一.带小数点数字 public static bool IsNumber(string input) { string pattern = "^-?\\d+$|^(-?\\d+)(\\.\\ ...

  6. javaweb之Cookie篇

    Cookie是在浏览器访问某个Web资源时,由Web服务器在Http响应消息头中通过Set-Cookie字段发送给浏览器的一组数据. 一个Cookie只能表示一个信息对,这个信息对有一个信息名(Nam ...

  7. JAVA工作方式

    public class Hellow { int eyes = 2; int ears = 2; int legs = 4; void run(){ //方法 System.out.println( ...

  8. 大开眼界 游览Facebook香港办公室

    想加入Facebook 的话不一定要跑去美国,Facebook在香港也开了一个很赞的办公室.除了无敌海景外,更可享用按摩椅.乒乓球桌.跑步机.麻将桌.酒廊.育婴室及开放式厨房.

  9. SharePoint 2013 开发——SharePoint Designer 2013工作流

    博客地址:http://blog.csdn.net/FoxDave SharePoint Designer 2013为开发者和高级用户提供了两种创建定制工作流的模式: 基于文本的设计器--即我们一直 ...

  10. SharePoint 2013 Nintex Workflow 工作流帮助(十一)

    博客地址 http://blog.csdn.net/foxdave 工作流动作 27. Create item in another site(Libraries and lists分组) 该操作用于 ...