ÔÎÄ£ºhttp://www.laruence.com/2011/12/30/2435.html
ÉÏһƪÎÄÕÂ, ÎÒ½éÉÜÁËÒ»¸öÀûÓÃHash³åÍ»(Åöײ)À´¶Ô¸÷ÖÖÓïÑÔ(°üÀ¨,PHP, Java, RubyµÈµÈ)ʵʩ¾Ü¾ø·þÎñ¹¥»÷µÄ¿ÉÄÜ, µ«ÊÇûÓиø³öʵÀý, ÎÄÕ·¢³öºó, @Ferrariͬѧ¸ø³öÁËÒ»¸öÁíÍâһƪÎÄÕÂSupercolliding a PHP array, ÎÄÕÂÖÐ×÷Õß½éÉÜÁËÒ»ÖÖ»ùÓÚPHPµÄ³åͻʵÀý, ÒÔ¼°´øÀ´µÄÐÔÄܶñ»¯¶Ô±È. ÎҾͽ軨Ï×·ð, ·Òë¸ø´ó¼Ò¿´¿´.
ÄãÖªµÀ²»ÖªµÀ, ²åÈë65536¸ö¾¹ý¹¹ÔìµÄ¼üÖµµÄÔªËص½PHPÊý×é, »áÐèÒªºÄʱ30ÃëÒÔÉÏ? ¶øÒ»°ãµÄÕâ¸ö¹ý³Ì½ö½öÐèÒª0.1Ãë..
Çë¿´ÈçϵÄÀý×Ó:
<?php $size = pow(2, 16); $startTime = microtime(true); $array = array(); for ($key = 0, $maxKey = ($size - 1) * $size; $key <= $maxKey; $key += $size) { $array[$key] = 0; } $endTime = microtime(true); echo '²åÈë ', $size, ' ¸ö¶ñÒâµÄÔªËØÐèÒª ', $endTime - $startTime, ' Ãë'."\n"; $startTime = microtime(true); $array = array(); for ($key = 0, $maxKey = $size - 1; $key <= $maxKey; ++$key) { $array[$key] = 0; } $endTime = microtime(true); echo '²åÈë ', $size, ' ¸öÆÕͨԪËØÐèÒª ', $endTime - $startTime, ' Ãë'."\n";
ÉÏÃæµÄÀý×Ó, ÔÚÎҵĻúÆ÷ÉϵÄÖ´Ðнá¹ûÈçÏÂ:
- ²åÈë 65536 ¸ö¶ñÒâµÄÔªËØÐèÒª 43.1438360214 Ãë
- ²åÈë 65536 ¸öÆÕͨԪËØÐèÒª 0.0210378170013
Õâ¸ö²î±ðÊDz»ÊǺܿäÕÅ?!
ÎÒÔÚÉÏһƪÎÄÕÂÖнéÉܹý, ¾¹ýÌØÊâ¹¹ÔìµÄ¼üÖµ, ʹµÃPHPÿһ´Î²åÈ붼»áÔì³ÉHash³åÍ», ´Ó¶øʹµÃPHPÖÐarrayµÄµ×²ãHash±íÍË»¯³ÉÁ´±í:
ÕâÑùÔÚÿ´Î²åÈëµÄʱºòPHP¶¼ÐèÒª±éÀúÒ»±éÕâ¸öÁ´±í, ´ó¼Ò¿ÉÒÔÏëÏó, µÚÒ»´Î²åÈë, ÐèÒª±éÀú0¸öÔªËØ, µÚ¶þ´ÎÊÇ1¸ö, µÚÈý´ÎÊÇ3¸ö, µÚ65536¸öÊÇ65535¸ö, ÄÇô×ܹ²¾ÍÐèÒª65534*65535/2=2147385345´Î±éÀú¡.
ÄÇô, Õâ¸ö¼üÖµÊÇÔõô¹¹ÔìµÄÄØ?
ÔÚPHPÖÐ,Èç¹û¼üÖµÊÇÊý×Ö, ÄÇôHashµÄʱºò¾ÍÊÇÊý×Ö±¾Éí, Ò»°ãµÄʱºò¶¼ÊÇ, index & tableMask. ¶øtableMaskÊÇÓÃÀ´±£Ö¤Êý×ÖË÷Òý²»»á³¬³öÊý×é¿ÉÈÝÄɵÄÔªËظöÊýÖµ, Ò²¾ÍÊÇÊý×é¸öÊý-1.
PHPµÄHashtableµÄ´óС¶¼ÊÇ2µÄÖ¸Êý, ±ÈÈçÈç¹ûÄã´æÈë10¸öÔªËصÄÊý×é, ÄÇôÊý×éʵ¼Ê´óСÊÇ16, Èç¹û´æÈë20¸ö, Ôòʵ¼Ê´óСΪ32, ¶ø63¸ö»°, ʵ¼Ê´óСΪ64. µ±ÄãµÄ´æÈëµÄÔªËظöÊý´óÓÚÁËÊý×éÄ¿Ç°µÄ×î¶àÔªËظöÊýµÄʱºò, PHP»á¶ÔÕâ¸öÊý×é½øÐÐÀ©ÈÝ, ²¢ÇÒ´ÓÐÂHash.
ÏÖÔÚ, ÎÒÃǼÙÉèÒª´æÈë64¸öÔªËØ(Öмä¿ÉÄܻᾹýÀ©ÈÝ, µ«ÊÇÎÒÃÇÖ»ÐèÒªÖªµÀ, ×îºóµÄÊý×é´óСÊÇ64, ²¢ÇÒ¶ÔÓ¦µÄtableMaskΪ63:0111111), ÄÇôÈç¹ûµÚÒ»´ÎÎÒÃÇ´æÈëµÄÔªËصļüֵΪ0, ÔòhashºóµÄֵΪ0, µÚ¶þ´ÎÎÒÃÇ´æÈë64, hash(1000000 & 0111111)µÄֵҲΪ0, µÚÈý´ÎÎÒÃÇÓÃ128, µÚËÄ´ÎÓÃ192¡ ¾Í¿ÉÒÔʹµÃµ×²ãµÄPHPÊý×é°ÑËùÓеÄÔªËض¼Hashµ½0ºÅbucketÉÏ, ´Ó¶øʹµÃHash±íÍË»¯³ÉÁ´±íÁË.
µ±È», Èç¹û¼üÖµÊÇ×Ö·û´®µÄ»°, ¾ÍÉÔ΢±È½ÏÂ鷳һЩÁË, µ«ÊÇPHPµÄHashËã·¨ÊÇ¿ªÔ´µÄ, ÒÑÖªµÄ, ËùÒÔÓÐÐÄÈËÒ²¿ÉÒÔ×öµ½¡
ÍƼöÐÅÏ¢
- ¡¾ÊÓƵ²¥·Å¡¿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¼ì²â×Ö·û´®±àÂë
ÆÀÂÛ