hash function比较
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 语言实现:
- unsigned int SDBMHash(char *str)
- {
- unsigned int hash = 0;
- while (*str)
- {
- // equivalent to: hash = 65599*hash + (*str++);
- hash = (*str++) + (hash << 6) + (hash << 16) - hash;
- }
- return (hash & 0x7FFFFFFF);
- }
- // RS Hash Function
- unsigned int RSHash(char *str)
- {
- unsigned int b = 378551;
- unsigned int a = 63689;
- unsigned int hash = 0;
- while (*str)
- {
- hash = hash * a + (*str++);
- a *= b;
- }
- return (hash & 0x7FFFFFFF);
- }
- // JS Hash Function
- unsigned int JSHash(char *str)
- {
- unsigned int hash = 1315423911;
- while (*str)
- {
- hash ^= ((hash << 5) + (*str++) + (hash >> 2));
- }
- return (hash & 0x7FFFFFFF);
- }
- // P. J. Weinberger Hash Function
- unsigned int PJWHash(char *str)
- {
- unsigned int BitsInUnignedInt = (unsigned int)(sizeof(unsigned int) * 8);
- unsigned int ThreeQuarters = (unsigned int)((BitsInUnignedInt * 3) / 4);
- unsigned int OneEighth = (unsigned int)(BitsInUnignedInt / 8);
- unsigned int HighBits = (unsigned int)(0xFFFFFFFF) << (BitsInUnignedInt - OneEighth);
- unsigned int hash = 0;
- unsigned int test = 0;
- while (*str)
- {
- hash = (hash << OneEighth) + (*str++);
- if ((test = hash & HighBits) != 0)
- {
- hash = ((hash ^ (test >> ThreeQuarters)) & (~HighBits));
- }
- }
- return (hash & 0x7FFFFFFF);
- }
- // ELF Hash Function
- unsigned int ELFHash(char *str)
- {
- unsigned int hash = 0;
- unsigned int x = 0;
- while (*str)
- {
- hash = (hash << 4) + (*str++);
- if ((x = hash & 0xF0000000L) != 0)
- {
- hash ^= (x >> 24);
- hash &= ~x;
- }
- }
- return (hash & 0x7FFFFFFF);
- }
- // BKDR Hash Function
- unsigned int BKDRHash(char *str)
- {
- unsigned int seed = 131; // 31 131 1313 13131 131313 etc..
- unsigned int hash = 0;
- while (*str)
- {
- hash = hash * seed + (*str++);
- }
- return (hash & 0x7FFFFFFF);
- }
- // DJB Hash Function
- unsigned int DJBHash(char *str)
- {
- unsigned int hash = 5381;
- while (*str)
- {
- hash += (hash << 5) + (*str++);
- }
- return (hash & 0x7FFFFFFF);
- }
- // AP Hash Function
- unsigned int APHash(char *str)
- {
- unsigned int hash = 0;
- int i;
- for (i=0; *str; i++)
- {
- if ((i & 1) == 0)
- {
- hash ^= ((hash << 7) ^ (*str++) ^ (hash >> 3));
- }
- else
- {
- hash ^= (~((hash << 11) ^ (*str++) ^ (hash >> 5)));
- }
- }
- return (hash & 0x7FFFFFFF);
- }
- https://www.byvoid.com/blog/string-hash-compare/
hash function比较的更多相关文章
- Hash function
Hash function From Wikipedia, the free encyclopedia A hash function that maps names to integers fr ...
- General Purpose Hash Function Algorithms
General Purpose Hash Function Algorithms post@: http://www.partow.net/programming/hashfunctions/inde ...
- 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 ...
- STL标准库-一个万用的hash function
技术在于交流.沟通,本文为博主原创文章转载请注明出处并保持作品的完整性 在前面我介绍过hash的使用,本次主要介绍一下Hash Function Hash Function即获得hash code的函 ...
- 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 ...
- 常用加密算法学习总结之散列函数(hash function)
散列函数(Hash function)又称散列算法.哈希函数,散列函数把消息或数据压缩成摘要,使得数据量变小,将数据的格式固定下来.该函数将数据打乱混合,重新创建一个叫做散列值(hash values ...
- 牛客多校第四场 J.Hash Function(线段树优化建图+拓扑排序)
题目传送门:https://www.nowcoder.com/acm/contest/142/J 题意:给一个hash table,求出字典序最小的插入序列,或者判断不合法. 分析: eg.对于序列{ ...
- hash function 字符串哈希函数
#include <stdio.h> int hash(const char *str) { ; ;;i++) { if (str[i] == '\0') break; sum += (( ...
- STL hash function的模板特化
哈希函数的作用是将一个值映射为一个哈希值,从而根据这个哈希值,在哈希表中对数据进行定位. template <class _Val, class _Key, class _HashFcn, cl ...
随机推荐
- windows 开启 nginx 监听80 端口 以及 禁用 http 服务后,无法重启 HTTP 服务,提示 系统错误 123,文件目录、卷标出错
1. 正常情况直接运行 start nginx.exe 不能开启成功,因为 80 端口被占用.提示: bind() to 0.0.0.0:80 failed (10013: An attempt w ...
- 构建高可靠hadoop集群之1-理解hdfs架构
本文主要参考 http://hadoop.apache.org/docs/r2.8.0/hadoop-project-dist/hadoop-hdfs/HdfsDesign.html 主要内容是对该文 ...
- 01javascript基础
1.JavaScript:直接写入 HTML 输出流 实例:(只能在 HTML 输出中使用 document.write.如果在文档加载后使用该方法,会覆盖整个文档) <!DOCTYPE htm ...
- MySQL数据库操作(DDL)
一.创建数据库 语法:create database 数据库名称 [库选项]; 库选项:(可选)数据库的属性,一般有字符集与校对集,保存在数据库所属文件夹下的opt文件 charset:字符集,表示该 ...
- 吐血分享:QQ群霸屏技术教程(利润篇)
QQ群技术,不论日进几百,空隙时间多的可以尝试,日进100问题不大. QQ群技术,如何赚钱,能赚多少钱?不同行业,不同关键词,不同力度,不一样的产出. 群费 群费,这个和付费群是有区别的,群费在手机端 ...
- 一个简易的android浏览器。求指教!
开发android的浏览器很简单吧,不过这浏览器倒是很简易.下面每一处代码都备注上注解了!废话不多说,下面直接上代码! 运行后界面 主界面的代码 activity_main.xml布局 <L ...
- 一笔画问题 南阳acm42(貌似没用到什么算法)
一笔画问题 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述 zyc从小就比较喜欢玩一些小游戏,其中就包括画一笔画,他想请你帮他写一个程序,判断一个图是否能够用一笔画下 ...
- java元注解(注解在注解上的注解)
//ElementType.TYPE 给类.接口.枚举上使用 @Target(ElementType.TYPE)//给注解进行注解,表示该注解可以用在什么地方 //@Retention(Retenti ...
- HDU暑假多校第四场J-Let Sudoku Rotate
一.题意 Sudoku is a logic-based, combinatorial number-placement puzzle, which is popular around the wor ...
- 插件开发遇到的坑------final 型变量,编译过程被优化
android 插件开发遇到的坑 今天遇到一个坑,pdf 插件,调用了主工程的一个静态final 字符串,但是主工程里面已经没有这个字符串了,却没有崩溃. 后来同事说,因为字符串可能已经直接被写死了. ...