ת×Ô£ºhttp://www.laruence.com/2009/07/23/994.html
Hash TableÊÇPHPµÄºËÐÄ,Õâ»°Ò»µã¶¼²»¹ý·Ö.
PHPµÄÊý×é,¹ØÁªÊý×é,¶ÔÏóÊôÐÔ,º¯Êý±í,·ûºÅ±í,µÈµÈ¶¼ÊÇÓÃHashTableÀ´×öΪÈÝÆ÷µÄ.
PHPµÄHashTable²ÉÓõÄÀÁ´·¨À´½â¾ö³åÍ», Õâ¸ö×Ô²»Óöà˵, ÎÒ½ñÌìÖ÷Òª¹Ø×¢µÄ¾ÍÊÇPHPµÄHashËã·¨, ºÍÕâ¸öËã·¨±¾Éí͸¶³öÀ´µÄһЩ˼Ïë.
PHPµÄHash²ÉÓõÄÊÇÄ¿Ç°×îΪÆÕ±éµÄDJBX33A (Daniel J. Bernstein, Times 33 with Addition), Õâ¸öËã·¨±»¹ã·ºÔËÓÃÓë¶à¸öÈí¼þÏîÄ¿,Apache, PerlºÍBerkeley DBµÈ. ¶ÔÓÚ×Ö·û´®¶øÑÔÕâÊÇÄ¿Ç°ËùÖªµÀµÄ×îºÃµÄ¹þÏ£Ëã·¨£¬ÔÒòÔÚÓÚ¸ÃËã·¨µÄËٶȷdz£¿ì£¬¶øÇÒ·ÖÀà·Ç³£ºÃ(³åͻС,·Ö²¼¾ùÔÈ).
Ëã·¨µÄºËÐÄ˼Ïë¾ÍÊÇ:
hash(i) = hash(i-1) * 33 + str[i]
ÔÚzend_hash.hÖÐ,ÎÒÃÇ¿ÉÒÔÕÒµ½ÔÚPHPÖеÄÕâ¸öËã·¨:
static inline ulong zend_inline_hash_func(char *arKey, uint nKeyLength) { register ulong hash = 5381; /* variant with the hash unrolled eight times */ for (; nKeyLength >= 8; nKeyLength -= <img src='/uploads/201112/201112311325324271_0.gif' alt='8)' class='wp-smiley' /> { hash = ((hash << 5) + hash) + *arKey++; hash = ((hash << 5) + hash) + *arKey++; hash = ((hash << 5) + hash) + *arKey++; hash = ((hash << 5) + hash) + *arKey++; hash = ((hash << 5) + hash) + *arKey++; hash = ((hash << 5) + hash) + *arKey++; hash = ((hash << 5) + hash) + *arKey++; hash = ((hash << 5) + hash) + *arKey++; } switch (nKeyLength) { case 7: hash = ((hash << 5) + hash) + *arKey++; /* fallthrough... */ case 6: hash = ((hash << 5) + hash) + *arKey++; /* fallthrough... */ case 5: hash = ((hash << 5) + hash) + *arKey++; /* fallthrough... */ case 4: hash = ((hash << 5) + hash) + *arKey++; /* fallthrough... */ case 3: hash = ((hash << 5) + hash) + *arKey++; /* fallthrough... */ case 2: hash = ((hash << 5) + hash) + *arKey++; /* fallthrough... */ case 1: hash = ((hash << 5) + hash) + *arKey++; break; case 0: break; EMPTY_SWITCH_DEFAULT_CASE() } return hash; }
Ïà±ÈÔÚApacheºÍPerlÖÐÖ±½Ó²ÉÓõľµäTimes 33Ëã·¨:
hashing function used in Perl 5.005: # Return the hashed value of a string: $hash = perlhash("key") # (Defined by the PERL_HASH macro in hv.h) sub perlhash { $hash = 0; foreach (split //, shift) { $hash = $hash*33 + ord($_); } return $hash; }
ÔÚPHPµÄhashËã·¨ÖÐ, ÎÒÃÇ¿ÉÒÔ¿´³öºÜ´¦Ï¸ÖµIJ»Í¬.
Ê×ÏÈ, ×î²»Ò»ÑùµÄ¾ÍÊÇ, PHPÖв¢Ã»ÓÐʹÓÃÖ±½Ó³Ë33, ¶øÊDzÉÓÃÁË:
hash << 5 + has
ÕâÑùµ±È»»á±ÈÓó˿ìÁË.
È»ºó, ÌرðÒªÖ÷ÒâµÄ¾ÍÊÇʹÓõÄunrolled, ÎÒÇ°¼¸Ìì¿´¹ýһƬÎÄÕ½²DiscuzµÄ»º´æ»úÖÆ, ÆäÖоÍÓÐÒ»Ìõ˵ÊÇDiscuz»á¸ù¾ÝÌû×ÓµÄÈȶȲ»Í¬²ÉÓò»Í¬µÄ»º´æ²ßÂÔ, ¸ù¾ÝÓû§Ï°¹ß,¶øÖ»»º´æÌû×ӵĵÚÒ»Ò³(ÒòΪºÜÉÙÓÐÈË»á·Ìû×Ó).
ÓÚ´ËÀàËƵÄ˼Ïë, PHP¹ÄÀø8λһϵÄ×Ö·ûË÷Òý, ËûÒÔ8Ϊµ¥Î»Ê¹ÓÃunrolledÀ´Ìá¸ßЧÂÊ, Õâ²»µÃ²»ËµÒ²ÊǸöºÜϸ½ÚµÄ,ºÜϸÖµĵط½.
ÁíÍ⻹ÓÐinline, register±äÁ¿ ¡ ¿ÉÒÔ¿´³öPHPµÄ¿ª·¢ÕßÔÚhashµÄÓÅ»¯ÉÏÒ²ÊÇÉ··Ñ¿àÐÄ
×îºó¾ÍÊÇ, hashµÄ³õʼֵÉèÖóÉÁË5381, Ïà±ÈÔÚApacheÖеÄtimesËã·¨ºÍPerlÖеÄHashËã·¨(¶¼²ÉÓóõʼhashΪ0), Ϊʲôѡ5381ÄØ? ¾ßÌåµÄÔÒòÎÒÒ²²»ÖªµÀ, µ«ÊÇÎÒ·¢ÏÖÁË5381µÄһЩÌØÐÔ:
Magic Constant 5381: 1. odd number 2. prime number 3. deficient number 4. 001/010/100/000/101 b
¿´ÁËÕâЩ, ÎÒÓÐÀíÓÉÏàÐÅÕâ¸ö³õʼֵµÄÑ¡¶¨ÄÜÌṩ¸üºÃµÄ·ÖÀà.
ÖÁÓÚ˵, ΪʲôÊÇTimes 33¶ø²»ÊÇTimes ÆäËûÊý×Ö, ÔÚPHP HashËã·¨µÄ×¢ÊÍÖÐÒ²ÓÐһЩ˵Ã÷, Ï£Íû¶ÔÓÐÐËȤµÄͬѧÓÐÓÃ:
DJBX33A (Daniel J. Bernstein, Times 33 with Addition) This is Daniel J. Bernstein's popular `times 33' hash function as posted by him years ago on comp.lang.c. It basically uses a function like ``hash(i) = hash(i-1) * 33 + str[i]''. This is one of the best known hash functions for strings. Because it is both computed very fast and distributes very well. The magic of number 33, i.e. why it works better than many other constants, prime or not, has never been adequately explained by anyone. So I try an explanation: if one experimentally tests all multipliers between 1 and 256 (as RSE did now) one detects that even numbers are not useable at all. The remaining 128 odd numbers (except for the number 1) work more or less all equally well. They all distribute in an acceptable way and this way fill a hash table with an average percent of approx. 86%. If one compares the Chi^2 values of the variants, the number 33 not even has the best value. But the number 33 and a few other equally good numbers like 17, 31, 63, 127 and 129 have nevertheless a great advantage to the remaining numbers in the large set of possible multipliers: their multiply operation can be replaced by a faster operation based on just one shift plus either a single addition or subtraction operation. And because a hash function has to both distribute good _and_ has to be very fast to compute, those few numbers should be preferred and seems to be the reason why Daniel J. Bernstein also preferred it. -- Ralf S. Engelschall <rse@engelschall.com>
ÍƼöÐÅÏ¢
- ¡¾ÊÓƵ²¥·Å¡¿JplayerÊÓƵ²¥·ÅÆ÷µÄʹÓÃ
- memcacheÄÚ´æÔÀí
- Memcache¼¼Êõ·ÖÏí£º½éÉÜ¡¢Ê¹Óᢴ洢¡¢Ëã·¨¡¢ÓÅ»¯....
- php³£ÓÃÕýÔò±í´ïʽ
- phpÐÔÄܼà²âÄ£¿éXHProf
- ÈÃCI¿ò¼ÜÖ§³Öservice²ã
- ʹÓÃPHPÉú³É´øLOGOµÄ¸öÐÔ»¯¶þάÂëͼÏñ
- ¹ØÓÚCodeIgniterÄã¿ÉÄܲ»ÖªµÀµÄ5¸ö֪ʶµã
- Memcache ºÁÃ뼶³¬Ê±¼°ÆäËû³£¼ûÎÊÌâ»ã×Ü
- [PHP±Ê¼Ç]PHPQueryÒ»¸ö´¦ÀíDOMµÄÀûÆ÷
ÈÈÃÅÐÅÏ¢
- nohup: redirecting stderr to stdou....
- ʹÓÃlog_formatΪNginx·þÎñÆ÷ÉèÖøüÏêϸµÄÈÕÖ¾¸ñʽ
- jquery easyUI--dataGrid-Json
- [Ô´´]·ÂGoogle Reader¡¢ÐÂÀË΢²©¡¢ÌÚѶ΢²©µ....
- ÀûÓÃKeepalived+mysql¹¹½¨¸ß¿ÉÓÃMySQLË«Ö÷×Ô¶....
- Nginx+keepalivedʵÏÖ¸ºÔؾùºâºÍË«»úÈȱ¸¸ß¿ÉÓÃ
- jqueryʵÏÖÒ³Ãæ¼ÓÔؽø¶ÈÌõ
- Rolling cURL: PHP²¢·¢×î¼Ñʵ¼ù
- codeigniter ·ÓÉÖÕ¼«ÓÅ»¯(url rewrite)
- linuxÏÂÉèÖÃsshÎÞÃÜÂëµÇ¼
×î½ü¸üÐÂ
- PHP»ñÈ¡Óû§µÄÕæʵIP£¬²¢ÅжÏÊÇ·ñÄÚÍøIP
- PHP ´íÎóÈÕÖ¾ error_log
- ÀûÓÃbigpipe»úÖÆʵÏÖÒ³ÃæÄ£¿éµÄÒì²½äÖȾ chunked¼¼Êõ
- php¿ØÖÆÎļþÏÂÔØËÙ¶È
- js + php ¶ÁÈ¡¡¢²¥·ÅÊÓƵÁ÷ ¼æÈÝfirefox£¬c....
- ¡¾ÊÓƵ²¥·Å¡¿JplayerÊÓƵ²¥·ÅÆ÷µÄʹÓÃ
- UNICODE Óë UTF-8 µÄ¹Øϵ
- memcacheÄÚ´æÔÀí
- Memcache¼¼Êõ·ÖÏí£º½éÉÜ¡¢Ê¹Óᢴ洢¡¢Ëã·¨¡¢ÓÅ»¯....
- phpʹÓÃmb_detect_encoding¼ì²â×Ö·û´®±àÂë
ÆÀÂÛ