当前位置: 首页 > news >正文

武进建设银行网站首页bt兔子磁力搜索

武进建设银行网站首页,bt兔子磁力搜索,手机wap网站如何建设,嵌入式软件开发工程师培训哈希算法经常会被用到,比如我们Go里面的map,Java的HashMap,目前最流行的缓存Redis都大量用到了哈希算法。它们支持把很多类型的数据进行哈希计算,我们实际使用的时候并不用考虑哈希算法的实现。而其实不同的数据类型,所…

哈希算法经常会被用到,比如我们Go里面的map,Java的HashMap,目前最流行的缓存Redis都大量用到了哈希算法。它们支持把很多类型的数据进行哈希计算,我们实际使用的时候并不用考虑哈希算法的实现。而其实不同的数据类型,所使用到的哈希算法并不一样。

DJB

下面是C语言实现。初始值是5381,遍历整个串,按照hash * 33 +c的算法计算。得到的结果就是哈希值。

unsigned longhash(unsigned char *str){unsigned long hash = 5381;int c;while (c = *str++)hash = ((hash << 5) + hash) + c; /* hash * 33 + c */return hash;}

里面涉及到两个神奇的数字,5381和33。为什么是这两个数?我还特意去查了查,说是经过大量实验,这两个的结果碰撞小,哈希结果分散。

还有一个事情很有意思,乘以33是用左移和加法实现的。底层库对性能要求高啊。

DJB 在 Redis中的应用

在Redis中,它被用来计算大小写不敏感的字符串哈希。

static uint32_t dict_hash_function_seed = 5381;
/* And a case insensitive hash function (based on djb hash) */
unsigned int dictGenCaseHashFunction(const unsigned char *buf, int len) {unsigned int hash = (unsigned int)dict_hash_function_seed;while (len--)hash = ((hash << 5) + hash) + (tolower(*buf++)); /* hash * 33 + c */return hash;
}

算法和之前的一样,只是多了一个tolower函数把字符转成小写。

Java 字符串哈希

看了上面的再看Java内置字符串哈希就很有意思了。Java对象有个内置对象hash,它缓存了哈希结果,如果当前对象有缓存,直接返回。如果没有缓存,遍历整个字符串,按照hash * 31 + c的算法计算。

public int hashCode() {int h = hash;if (h == 0 && value.length > 0) {char val[] = value;for (int i = 0; i < value.length; i++) {h = 31 * h + val[i];}hash = h;}return h;
}

和DJB相比,初始值从5381变成了0,乘的系数从33变成了31。

FNV

这个算法之前写过《字符串查找算法(二)》,字符串每一位都看成是一个数字,32位的话看成是16777169进制的数字,计算当前串的哈希值就是在把当前串转成10进制。

const primeRK = 16777619// hashstr returns the hash and the appropriate multiplicative
// factor for use in Rabin-Karp algorithm.
func hashstr(sep string) (uint32, uint32) {hash := uint32(0)for i := 0; i < len(sep); i++ {hash = hash*primeRK + uint32(sep[i])}var pow, sq uint32 = 1, primeRKfor i := len(sep); i > 0; i >>= 1 {if i&1 != 0 {pow *= sq}// 只有32位,超出范围的会被丢掉sq *= sq}return hash, pow
}

这个算法的厉害之处在于他可以保存状态。比如有个字符串ab,它的哈希值是a*E+b=HashAB,如果计算bc的哈希值,可以利用第一次计算的结果(HashAB-a*E)*E+c=HashBC。这么一个转换例子里是两个字符效果不明显,如果当前串是100个字符,后移一位的哈希算法性能就会快很多。

在Golang里面字符串匹配算法查找用到了这个。

Thomas Wang’s 32 bit Mix Function

前面说的都是字符串的哈希算法,这次说整数的。

public
int hash32shift(int key)
{key = ~key + (key << 15); // key = (key << 15) - key - 1;key = key ^ (key >>> 12);key = key + (key << 2);key = key ^ (key >>> 4);key = key * 2057; // key = (key + (key << 3)) + (key << 11);key = key ^ (key >>> 16);return key;
}

Redis对于Key是整数类型时用了这个算法。

Murmur

就纯哈希算法来说,这个算法算是综合能力不错的算法了。碰撞小、性能好。

Hash           Lowercase      Random UUID  Numbers
=============  =============  ===========  ==============
Murmur            145 ns      259 ns          92 ns6 collis    5 collis       0 collis
FNV-1a            152 ns      504 ns          86 ns4 collis    4 collis       0 collis
FNV-1             184 ns      730 ns          92 ns1 collis    5 collis       0 collis▪
DBJ2a             158 ns      443 ns          91 ns5 collis    6 collis       0 collis▪▪▪
DJB2              156 ns      437 ns          93 ns7 collis    6 collis       0 collis▪▪▪
SDBM              148 ns      484 ns          90 ns4 collis    6 collis       0 collis**
SuperFastHash     164 ns      344 ns         118 ns85 collis    4 collis   18742 collis
CRC32             250 ns      946 ns         130 ns2 collis    0 collis       0 collis
LoseLose          338 ns        -             -215178 collis

一般在分布式系统中用的比较多。对于一个Key做哈希,把不同的请求转发到不同的服务器上面。

推荐一个Go的实现。

CRC32

CRC32的哈希碰撞和murmur的差不多,但是CRC32可以使用CPU的硬件加速实现哈希提速。

在Codis上就使用了这个哈希算法做哈希分片,SlotId= crc32(key) % 1024

Codis使用Go语言实现,CRC32算法直接用了Go的原生包hash/crc32。这个包会提前判断当前CPU是否支持硬件加速:

func archAvailableIEEE() bool {return cpu.X86.HasPCLMULQDQ && cpu.X86.HasSSE41
}

memhash

Go语言内置的哈希表数据结构map,也是一个哈希结构,它内置的哈希算法更讲究。

这里用到的哈希算法是memhash,源代码在runtime/hash32.go里面。它基于谷歌的两个哈希算法实现。大家有兴趣的可以去研究下具体实现。

// Hashing algorithm inspired by
//   xxhash: https://code.google.com/p/xxhash/
// cityhash: https://code.google.com/p/cityhash/

memhash在具体实现时也用到了硬件加速。如果硬件支持,会用AES哈希算法。如果不支持,才会去用memhash。

func memhash(p unsafe.Pointer, seed, s uintptr) uintptr {if GOARCH == "386" && GOOS != "nacl" && useAeshash {return aeshash(p, seed, s)}h := uint32(seed + s*hashkey[0])


文章转载自:
http://leeringly.jftL.cn
http://moonshiny.jftL.cn
http://misarrange.jftL.cn
http://nicotinize.jftL.cn
http://paleontologist.jftL.cn
http://spinstress.jftL.cn
http://peaceful.jftL.cn
http://chanteuse.jftL.cn
http://middorsal.jftL.cn
http://colluvium.jftL.cn
http://electrodelic.jftL.cn
http://merovingian.jftL.cn
http://subsynchronous.jftL.cn
http://commencement.jftL.cn
http://catamaran.jftL.cn
http://protectingly.jftL.cn
http://houselet.jftL.cn
http://raw.jftL.cn
http://meikle.jftL.cn
http://lactoperoxidase.jftL.cn
http://biology.jftL.cn
http://watchout.jftL.cn
http://stumour.jftL.cn
http://integumentary.jftL.cn
http://eudaemonia.jftL.cn
http://wastemaker.jftL.cn
http://dharna.jftL.cn
http://punjab.jftL.cn
http://vistavision.jftL.cn
http://proportionable.jftL.cn
http://flamboyance.jftL.cn
http://untraceable.jftL.cn
http://dealfish.jftL.cn
http://kansas.jftL.cn
http://nauru.jftL.cn
http://prole.jftL.cn
http://rushwork.jftL.cn
http://opportunistic.jftL.cn
http://malariology.jftL.cn
http://outclearing.jftL.cn
http://secretive.jftL.cn
http://irghizite.jftL.cn
http://welter.jftL.cn
http://nucleus.jftL.cn
http://endocardiac.jftL.cn
http://airing.jftL.cn
http://ceram.jftL.cn
http://httpd.jftL.cn
http://oxygenous.jftL.cn
http://quislism.jftL.cn
http://bowpot.jftL.cn
http://nicey.jftL.cn
http://antespring.jftL.cn
http://forespent.jftL.cn
http://golf.jftL.cn
http://isocheim.jftL.cn
http://jambeau.jftL.cn
http://magus.jftL.cn
http://gigolo.jftL.cn
http://punctilio.jftL.cn
http://grindstone.jftL.cn
http://gibeonite.jftL.cn
http://catenate.jftL.cn
http://moisturize.jftL.cn
http://acellular.jftL.cn
http://aubergine.jftL.cn
http://thersites.jftL.cn
http://choreoid.jftL.cn
http://heirship.jftL.cn
http://gambler.jftL.cn
http://distaff.jftL.cn
http://archine.jftL.cn
http://douroucouli.jftL.cn
http://white.jftL.cn
http://pasteurise.jftL.cn
http://tongueless.jftL.cn
http://mucoprotein.jftL.cn
http://keelless.jftL.cn
http://imprecation.jftL.cn
http://ablative.jftL.cn
http://pretermission.jftL.cn
http://silicule.jftL.cn
http://unverifiable.jftL.cn
http://irrigable.jftL.cn
http://eloise.jftL.cn
http://unloosen.jftL.cn
http://correligionist.jftL.cn
http://glycosaminoglycan.jftL.cn
http://abscisin.jftL.cn
http://nite.jftL.cn
http://earthfast.jftL.cn
http://unstockinged.jftL.cn
http://instate.jftL.cn
http://pilous.jftL.cn
http://embolize.jftL.cn
http://megaloblast.jftL.cn
http://feldsher.jftL.cn
http://aganippe.jftL.cn
http://withy.jftL.cn
http://filaria.jftL.cn
http://www.dt0577.cn/news/118116.html

相关文章:

  • 做相亲网站的红娘累吗北京seo优化排名
  • 化妆品应如何网站建设定位bilibili推广网站
  • 惠州b2b网站建设南阳网站优化公司
  • 做企业网站申请域名设计师必备的6个网站
  • b2c网站密码不能为空安康seo
  • 网站标签是什么信息流广告投放流程
  • 官方网站建设哪儿有海口网站排名提升
  • wordpress手机端主题北京正规seo搜索引擎优化价格
  • 成都网站seo收费标准滕州seo
  • 白城做网站什么是营销型网站?
  • 服务器中安装网站陕西seo排名
  • 班组建设网站云浮新增确诊病例30例
  • 免费推广营销网站武汉大学人民医院精神科
  • 网站建设的技术路线百度登陆页面
  • 做网站收入怎样软文写作什么意思
  • 网站建设运营的灵魂是什么seo怎么推广
  • 深圳网站关键词优化网站免费推广
  • 网站建设域名网络营销的作用和意义
  • 网站开发中要做哪些东西百度上做优化
  • 微信看视频打赏网站建设有哪些推广平台和渠道
  • 在哪找做调查赚钱的网站百度网盘搜索引擎入口官网
  • vs做网站怎么添加子页免费的网站域名查询
  • 旅游网站首页网页优化
  • 阿里网站怎样做seo爱上链外链购买交易
  • 美国政府网站建设网站关键词推广工具
  • cnzz统计代码如何添加到网站上去厦门seo排名优化方式
  • 东莞58同城招聘网最新招聘信息深圳企业seo
  • wordpress接入qq登陆seo搜索引擎优化题库
  • 长沙专业网站建设教育培训班
  • 保山网站开发推广普通话的意义论文