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. mysql 5.7 zip 文件在 windows下的安装

    1.下载mysql最新版本. http://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.15-winx64.zip 2.解压到文件夹. D:\softwa ...

  2. linux 一些命令

    1.查看cpu个数 cat /proc/cpuinfo |grep "physical id" |sort | uniq |wc -l 2 2.查看cpu逻辑个数 cat /pro ...

  3. 一道数学题 hduacm 5144

    题目大意:  一直初速度v和抛出速度h  求标枪抛出的最远距离: 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5144 显然  d=v/g*sqrt( ...

  4. POJ 2594 传递闭包的最小路径覆盖

    Treasure Exploration Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 7171   Accepted: 2 ...

  5. Deep Learning 初识

    实际生活中,人们为了解决一个问题,如对象的分类(对象可是是文档.图像等),首先必须做的事情是如何来表达一个对象,即必须抽取一些特征来表示一个对象,如文本的处理中,常常用词**来表示一个文档,或把文档表 ...

  6. Win7 Print Spooler服務自动关闭

    对于Win7系统而言,该问题通常是安装了错误的打印驱动引起的,Win7系统为了保护其它进程不受干扰,自动关闭了打印服务. 解决方法就是: a> 把不用的打印机删掉. b> 确保你安装了正确 ...

  7. 5个最顶级jQuery图表类库插件-Charting plugin

    转载: http://www.cnblogs.com/chu888chu888/archive/2012/12/22/2828962.html 作者:Leonel Hilario翻译:Terry li ...

  8. 第一次使用Git心得体会

    用书本上的概念讲,Git是一个分布式的版本控制工具,每一个Git的工作目录都是一个完全独立的代码库,并拥有完整的历史记录和版本追踪能力,能够不依赖于网络和中心服务器.也就是说Git能够不需要服务器而在 ...

  9. plist 和 Xib

    plist文件 mainbudin加载时候有后缀 xib文件  mainbudin加载时候无需后缀

  10. io函数

    io函数一般分为两大类: 系统(不带缓存)调用: 如read.write.open 标准(带缓存)调用: fread.fwrite.fopen 上面说的带缓存/不带缓存是针对用户态的,内核态本身都是带 ...