{"id":510,"date":"2010-08-23T10:04:55","date_gmt":"2010-08-23T10:04:55","guid":{"rendered":""},"modified":"2010-08-23T10:04:55","modified_gmt":"2010-08-23T10:04:55","slug":"510","status":"publish","type":"post","link":"https:\/\/www.poloo.org\/?p=510","title":{"rendered":"\u7b80\u5355\u7684php\u6269\u5c55\u4ecb\u7ecd\u4e0e\u5f00\u53d1"},"content":{"rendered":"<p>\u6211\u4eec\u4f7f\u7528php\u6269\u5c55\uff0c\u4e3b\u8981\u76ee\u7684\u662f\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6267\u884c\u6548\u7387\uff0c\u5bf9\u4e8e\u8bbf\u95ee\u91cf\u5f88\u5927\u7684\u4ee3\u7801\u6216\u8005\u903b\u8f91\u5c06\u5176\u5199\u6210\u6269\u5c55\u3002\u5728\u505a\u9879\u76ee\u7684\u8fc7\u7a0b\u4e2d\uff0c\u9700\u8981\u5bf9\u6570\u636e\u8fdb\u884c\u6392\u5e8f\uff0c\u6570\u636e\u8fd0\u7b97\u6bd4\u8f83\u590d\u6742\uff1b\u6211\u4eec\u51c6\u5907\u5bf9\u4e00\u767e\u4e07\u4e2a\u6570\u636e\u8fdb\u884c\u6392\u5e8f\uff0c \u4e0b\u9762\u662f\u6211\u5728\u7a0b\u5e8f\u4e4b\u524d\u505a\u7684\u4e00\u4e2a\u6d4b\u8bd5\uff1a\u9996\u5148\u4f7f\u7528php\u7a0b\u5e8f\u751f\u6210\u4e00\u767e\u4e07\u7684\u968f\u673a\u6570\uff0c\u5e76\u5c06\u5176\u4fdd\u5b58\u5728\u6587\u4ef6\u4e2d\u3002<\/p>\n<p>\u751f\u6210\u968f\u5373\u6570\u7684\u4ee3\u7801\u5982\u4e0b\u9762\u6240\u793a\uff1a<\/p>\n<p><code><br \/>set_time_limit(0);<br \/>ini_set(\"memory_limit\", -1);<br \/>$data = array();<br \/>for($i = 1; $i &lt; 1000000; $i++)<br \/>\u3000\u3000$data[] = rand();file_put_contents('data.php', '&lt;?php $data = ' . var_export($data, true) . \"; ?&gt;\");<br \/><\/code><\/p>\n<p>\u4ee3\u7801\u5f88\u7b80\u5355\uff0c\u4e00\u770b\u5c31\u660e\u767d\uff0c\u8fd9\u91cc\u4e0d\u4f5c\u8bf4\u660e\u3002<\/p>\n<p>\u4e0b\u9762\u662f\u4f7f\u7528php\u5199\u7684\u5feb\u901f\u6392\u5e8f\uff0cphp\u81ea\u8eab\u5e26\u7684\u6392\u5e8f\u51fd\u6570\uff0c\u4ee5\u53ca\u81ea\u5df1\u6269\u5c55\u7684\u6392\u5e8f\u51fd\u6570\uff0c\u5b83\u4eec\u6240\u9700\u65f6\u95f4\u5982\u4e0b\u6240\u793a\uff1a<br \/><img decoding=\"async\" src=\"http:\/\/www.poloo.org\/wp-content\/attachments\/month_1008\/q20108231018.png\" \/><\/p>\n<p>\u4e0b\u9762\u5c06php\u4ee3\u7801\u5c55\u793a\u4e00\u4e0b\uff0c\u9700\u8981\u8bf4\u660e\u7684\u662f\uff1ahello\u51fd\u6570\u65f6\u81ea\u5df1\u5199\u7684\u6269\u5c55<\/p>\n<p><code><br \/>&lt;?phpini_set(\"memory_limit\", -1);<br \/>set_time_limit(0);<br \/>include_once('data.php');&nbsp;&nbsp; \/\/\u521a\u624d\u4ea7\u751f\u7684\u968f\u673a\u6570\u4fdd\u5b58\u5728\u8fd9\u4e2a\u6587\u4ef6<br \/>$len = count($data);<br \/>$data_s = $data_q = $data;<br \/>$s_s = $s_t = array_sum(explode(\" \", microtime()));<br \/>qsort($data, 0, $len-1);<br \/>$s_t = array_sum(explode(\" \", microtime()));<br \/>sort($data_s);<br \/>$q_t = array_sum(explode(\" \", microtime()));<br \/>$data_q = hello($data_q);<br \/>$r_t = array_sum(explode(\" \", microtime()));<br \/>echo \"php\u5199\u7684\u5feb\u901f\u6392\u5e8f\u65f6\u95f4:&nbsp;&nbsp;&nbsp;&nbsp;\" . ($s_t-$s_s) . \"&lt;br&gt;\";<br \/>echo \"\u7cfb\u7edf\u6392\u5e8f\u51fd\u6570\u4f7f\u7528\u65f6\u95f4\u4e3a: \" . ($q_t-$s_t) . \"&lt;br&gt;\";<br \/>echo \"\u672c\u5730\u6392\u5e8f\u51fd\u6570\u4f7f\u7528\u65f6\u95f4\u4e3a: \" . ($r_t-$q_t) . \"&lt;br&gt;\";<br \/>echo \"\u4e24\u4e2a\u7ed3\u679c\u7684\u5bf9\u6bd4\uff1a\" . ($data_s === $data_q);<br \/>function qsort(&$arr, $l, $u){<br \/>&nbsp;&nbsp;&nbsp;&nbsp;if($l &gt;= $u)<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;$m = $l;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;for($i = $l+1; $i&lt;=$u; $i++)<br \/>&nbsp;&nbsp;&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if($arr[$i] &lt; $arr[$l]) <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$m++;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if($m != $i)<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$t = $arr[$i];<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$arr[$i] = $arr[$m];<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$arr[$m] = $t;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>&nbsp;&nbsp;&nbsp;&nbsp;$t = $arr[$l];<br \/> $arr[$l] = $arr[$m];<br \/> $arr[$m] = $t;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;qsort($arr, $l, $m-1);<br \/>&nbsp;&nbsp;&nbsp;&nbsp;qsort($arr, $m+1, $u);<br \/>}?&gt;<br \/><\/code><\/p>\n<p>\u5f88\u7b80\u5355\u5427\uff0c \u591a\u7684\u4e0d\u8bf4\u5566\uff0c\u4e0b\u9762\u8bf4\u8bf4\u8fd9\u4e2a\u6269\u5c55\u7684\u5f00\u53d1<\/p>\n<p>\u751f\u6210 \u6269\u5c55\u6709\u4e00\u5b9a\u7684\u6b65\u9aa4\uff0c\u7f51\u4e0a\u90fd\u6709\u5f88\u8bb2\u89e3\uff0c\u6211\u8fd9\u91cc\u4e5f\u4e0d\u518d\u7f57\u55e6\uff0c<\/p>\n<p>\u9996\u5148\u4f7f\u7528php\u7684\u5e93\u6587\u4ef6\u751f\u6210\u4e00\u4e2a\u57fa\u672c\u7684\u6269\u5c55\u9879\u76ee<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.poloo.org\/wp-content\/attachments\/month_1008\/e201082310449.png\" \/><\/p>\n<p>\u751f\u6210\u7684\u9879\u76ee\uff0c<\/p>\n<p>\u4fee\u6539sort.c\uff0c\u5c06\u81ea\u5df1\u7684\u5feb\u6392\u5199\u8fdb\u53bb\uff0c \u6211\u8fd9\u8fb9\u7684\u4ee3\u7801\u662f\u5c06php\u7a0b\u5e8f\u7684\u5feb\u901f\u6392\u5e8f\u6539\u6210c\u8bed\u8a00\u3002\u751f\u6210dll\u540e\uff0c\u5c06\u5176\u653e\u5728php\u6269\u5c55\u4e2d\uff0c\u9996\u5148\u68c0\u67e5\u4e00\u4e0b\u662f\u5426\u5df2\u5c06\u542f\u7528\u6269\u5c55\u3002<\/p>\n<p>\u7136\u540e \u5728\u4ee3\u7801\u4e2d\u76f4\u63a5\u4f7f\u7528hello\u51fd\u6570\uff08\u6211\u8fd9\u91cc\u6ca1\u6709\u6539\u51fd\u6570\u540d\uff09<\/p>\n<p>\u6ce8\u610f\uff1a\u6269\u5c55\u4e00\u5b9a\u8981\u8003\u8651\u5168\u9762\uff0c\u4e0d\u80fd\u5728\u4f7f\u7528\u4e2d\u51fa\u9519\uff0c\u7279\u522b\u662fC\u5199\u7684\u4ee3\u7801\uff0c\u5426\u5219\u4f1a\u51fa\u73b0php\u7cfb\u7edf\u5d29\u6e83<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6211\u4eec\u4f7f\u7528php\u6269\u5c55\uff0c\u4e3b\u8981\u76ee\u7684\u662f\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6267\u884c\u6548\u7387\uff0c\u5bf9\u4e8e\u8bbf\u95ee\u91cf\u5f88\u5927\u7684\u4ee3&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[],"class_list":["post-510","post","type-post","status-publish","format-standard","hentry","category-Php"],"_links":{"self":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts\/510","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=510"}],"version-history":[{"count":0,"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts\/510\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=510"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=510"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=510"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}