http://blog.csdn.net/kingstar158/article/details/8028635

由于工作需要,针对千万级别的数据,使用stl::map着实存在着效率问题,最后使用boost::unordered_map替代前者,发现效率上有很大的提升,但是还是无法达到我们的需求;

stl::map  底层算法:B+tree 实现

boost::unordered_map 底层算法:hash 实现

所以可能要针对不同的数据类型编写hash function来优化查找和插入的效率,自己编写,着实没有这个实力,只有在google上来寻找先辈们的精妙算法来借鉴:

常用字符串哈希函数有BKDRHash,APHash,DJBHash,JSHash,RSHash,SDBMHash,PJWHash,ELFHash等等;

有前辈做了评测:如下

ash函数 数据1 数据2 数据3 数据4 数据1得分 数据2得分 数据3得分 数据4得分 平均分
BKDRHash 2 0 4774 481 96.55 100 90.95 82.05 92.64
APHash 2 3 4754 493 96.55 88.46 100 51.28 86.28
DJBHash 2 2 4975 474 96.55 92.31 0 100 83.43
JSHash 1 4 4761 506 100 84.62 96.83 17.95 81.94
RSHash 1 0 4861 505 100 100 51.58 20.51 75.96
SDBMHash 3 2 4849 504 93.1 92.31 57.01 23.08 72.41
PJWHash 30 26 4878 513 0 0 43.89 0 21.95
ELFHash 30 26 4878 513 0 0 43.89 0 21.95

其中数据1为100000个字母和数字组成的随机串哈希冲突个数。数据2为100000个有意义的英文句子哈希冲突个数。数据3为数据1的哈希值与1000003(大素数)求模后存储到线性表中冲突的个数。数据4为数据1的哈希值与10000019(更大素数)求模后存储到线性表中冲突的个数。

各种hash function的C 语言实现:

    1. unsigned int SDBMHash(char *str)
    2. {
    3. unsigned int hash = 0;
    4. while (*str)
    5. {
    6. // equivalent to: hash = 65599*hash + (*str++);
    7. hash = (*str++) + (hash << 6) + (hash << 16) - hash;
    8. }
    9. return (hash & 0x7FFFFFFF);
    10. }
    11. // RS Hash Function
    12. unsigned int RSHash(char *str)
    13. {
    14. unsigned int b = 378551;
    15. unsigned int a = 63689;
    16. unsigned int hash = 0;
    17. while (*str)
    18. {
    19. hash = hash * a + (*str++);
    20. a *= b;
    21. }
    22. return (hash & 0x7FFFFFFF);
    23. }
    24. // JS Hash Function
    25. unsigned int JSHash(char *str)
    26. {
    27. unsigned int hash = 1315423911;
    28. while (*str)
    29. {
    30. hash ^= ((hash << 5) + (*str++) + (hash >> 2));
    31. }
    32. return (hash & 0x7FFFFFFF);
    33. }
    34. // P. J. Weinberger Hash Function
    35. unsigned int PJWHash(char *str)
    36. {
    37. unsigned int BitsInUnignedInt = (unsigned int)(sizeof(unsigned int) * 8);
    38. unsigned int ThreeQuarters  = (unsigned int)((BitsInUnignedInt  * 3) / 4);
    39. unsigned int OneEighth      = (unsigned int)(BitsInUnignedInt / 8);
    40. unsigned int HighBits        = (unsigned int)(0xFFFFFFFF) << (BitsInUnignedInt - OneEighth);
    41. unsigned int hash            = 0;
    42. unsigned int test            = 0;
    43. while (*str)
    44. {
    45. hash = (hash << OneEighth) + (*str++);
    46. if ((test = hash & HighBits) != 0)
    47. {
    48. hash = ((hash ^ (test >> ThreeQuarters)) & (~HighBits));
    49. }
    50. }
    51. return (hash & 0x7FFFFFFF);
    52. }
    53. // ELF Hash Function
    54. unsigned int ELFHash(char *str)
    55. {
    56. unsigned int hash = 0;
    57. unsigned int x  = 0;
    58. while (*str)
    59. {
    60. hash = (hash << 4) + (*str++);
    61. if ((x = hash & 0xF0000000L) != 0)
    62. {
    63. hash ^= (x >> 24);
    64. hash &= ~x;
    65. }
    66. }
    67. return (hash & 0x7FFFFFFF);
    68. }
    69. // BKDR Hash Function
    70. unsigned int BKDRHash(char *str)
    71. {
    72. unsigned int seed = 131; // 31 131 1313 13131 131313 etc..
    73. unsigned int hash = 0;
    74. while (*str)
    75. {
    76. hash = hash * seed + (*str++);
    77. }
    78. return (hash & 0x7FFFFFFF);
    79. }
    80. // DJB Hash Function
    81. unsigned int DJBHash(char *str)
    82. {
    83. unsigned int hash = 5381;
    84. while (*str)
    85. {
    86. hash += (hash << 5) + (*str++);
    87. }
    88. return (hash & 0x7FFFFFFF);
    89. }
    90. // AP Hash Function
    91. unsigned int APHash(char *str)
    92. {
    93. unsigned int hash = 0;
    94. int i;
    95. for (i=0; *str; i++)
    96. {
    97. if ((i & 1) == 0)
    98. {
    99. hash ^= ((hash << 7) ^ (*str++) ^ (hash >> 3));
    100. }
    101. else
    102. {
    103. hash ^= (~((hash << 11) ^ (*str++) ^ (hash >> 5)));
    104. }
    105. }
    106. return (hash & 0x7FFFFFFF);
    107. }
    108. https://www.byvoid.com/blog/string-hash-compare/

hash function比较的更多相关文章

  1. Hash function

    Hash function From Wikipedia, the free encyclopedia   A hash function that maps names to integers fr ...

  2. General Purpose Hash Function Algorithms

    General Purpose Hash Function Algorithms post@: http://www.partow.net/programming/hashfunctions/inde ...

  3. Lintcode: Hash Function && Summary: Modular Multiplication, Addition, Power && Summary: 长整形long

    In data structure Hash, hash function is used to convert a string(or any other type) into an integer ...

  4. STL标准库-一个万用的hash function

    技术在于交流.沟通,本文为博主原创文章转载请注明出处并保持作品的完整性 在前面我介绍过hash的使用,本次主要介绍一下Hash Function Hash Function即获得hash code的函 ...

  5. You shouldn't use *any* general-purpose hash function for user passwords, not BLAKE2, and not MD5, SHA-1, SHA-256, or SHA-3

    hashlib - Secure hashes and message digests - Python 3.8.3 documentation https://docs.python.org/3.8 ...

  6. 常用加密算法学习总结之散列函数(hash function)

    散列函数(Hash function)又称散列算法.哈希函数,散列函数把消息或数据压缩成摘要,使得数据量变小,将数据的格式固定下来.该函数将数据打乱混合,重新创建一个叫做散列值(hash values ...

  7. 牛客多校第四场 J.Hash Function(线段树优化建图+拓扑排序)

    题目传送门:https://www.nowcoder.com/acm/contest/142/J 题意:给一个hash table,求出字典序最小的插入序列,或者判断不合法. 分析: eg.对于序列{ ...

  8. hash function 字符串哈希函数

    #include <stdio.h> int hash(const char *str) { ; ;;i++) { if (str[i] == '\0') break; sum += (( ...

  9. STL hash function的模板特化

    哈希函数的作用是将一个值映射为一个哈希值,从而根据这个哈希值,在哈希表中对数据进行定位. template <class _Val, class _Key, class _HashFcn, cl ...

随机推荐

  1. Python常用模块之time和datetime

    1.时间的格式化 结构化时间 ##把字符串时间转换成结构化时间 time.strptime("2017-06-21","%Y-%m-%d") ##把结构化时间转 ...

  2. 怎样在Swift中使用CocoaPods

    怎样在Swift中使用CocoaPods 它不是神秘的亚马逊区域的部落人用手捡出来的生可可的豆荚,肯定不是!让CocoaPods website来回答可能是最好的: CocoaPods是Cocoa项目 ...

  3. mysql系列一

    学习mysql必备工具即安装mysql客户端:mysql安装教程在网上有很多,在此处就不在仔细说明: 下面将仔细介绍一下关于SQL语句: SQL语句:结构化查询语言(Structured Query ...

  4. MyEclipse格式化JSP代码导致Java表达式<%= %>自动换行的解决办法

    MyEclipse格式化JSP代码导致Java表达式<%= %>自动换行的解决办法: 可以将Java表达式<%= %>换成EL表达式.

  5. React学习(2)—— 组件的运用和数据传递

    React官方中文文档地址:    https://doc.react-china.org/ 了解了组件之后,就需要理解“Props”和“State”的用法.首先来介绍State,State按照字面意 ...

  6. 数据解压及if else的应用

    def sum(items): head, *tails = items return head + sum(tails) if tails else head # 最后一句有点像三目运算符,如果ta ...

  7. Spring笔记1

    Spring Spring特点 1. 方便解耦,简化开发 通过Spring提供的IoC容器,我们可以将对象之间的依赖关系交由Spring进行控制,避免硬编码所造成的过度程序耦合.有了Spring,用户 ...

  8. 数据分析处理库Pandas——索引进阶

    Series结构 筛选数据 指定值 备注:查找出指定数值的索引和数值. 逻辑运算 备注:查找出值大于2的数据. 复合索引 DataFrame结构 显示指定列 筛选显示 备注:值小于0的显示原值,否则显 ...

  9. PHP中的mysql_unbuffered_query与mysql_query的区别

    对于mysql_query大家都很熟悉,下面先简单介绍下mysql_unbuffered_query mysql_unbuffered_query (PHP 4 >= 4.0.6, PHP 5) ...

  10. C语言实例解析精粹学习笔记——32

    实例32: 编制一个包含姓名.地址.邮编和电话的通讯录输入和输出函数. 思路解析: 1.用结构体来完成姓名.地址.邮编和电话的组合. 2.结构体指针的使用. 3.malloc的使用 4.scanf函数 ...