php排序1亿个QQ号码
吃饱喝足了,还发贴了。
拆开分成几千份进行排序再合并。
首先先创建一个1亿个QQ号的txt。
<br /> <?php<br /> <br /> // 创建一亿个QQ号的txt (大约需85~100秒)<br /> <br /> set_time_limit(0);<br /> $fn = 'qq.txt';<br /> $fp = fopen($fn, 'w');<br /> <br /> $st = microtime(true);<br /> <br /> $l = range(0,10000);<br /> shuffle($l);<br /> foreach ($l as $k=>$v)<br /> {<br /> $arr = range($v*10000+10000,10000*($v+1)+9999);<br /> shuffle($arr);<br /> fputs($fp,implode("\n", $arr)."\n");<br /> unset($arr);<br /> }<br /> <br /> echo microtime(true)-$st;<br /> <br /> ?><br />
<br /> <?php<br /> <br /> // 长度号码分类 (大约需360~400秒)<br /> <br /> set_time_limit(0);<br /> $st = microtime(true);<br /> <br /> if(!is_dir('qq_no')) mkdir('qq_no');<br /> $file = fopen('qq.txt', 'r'); <br /> <br /> <br /> $i=0;<br /> $end_s = '';<br /> while(!feof($file))<br /> {<br /> $g = 1042*1024;<br /> fseek($file,$g*$i);<br /> $s = fread($file, $g);<br /> <br /> <br /> $end = strrpos($s, "\n");<br /> $arr_s = $end_s.substr($s, 0, $end);<br /> $end_s = substr($s, $end);<br /> <br /> $arr = explode("\n", $arr_s);<br /> foreach ($arr as $k=>$v)<br /> {<br /> if($v!='')<br /> {<br /> $tag = "$v[0]$v[1]$v[2]";<br /> $text_arr[strlen($v)][$tag][] = $v;<br /> }<br /> }<br /> <br /> foreach ($text_arr as $k=>$v)<br /> {<br /> $n_dir = 'qq_no/'.$k;<br /> if (!is_dir($n_dir)) mkdir($n_dir);<br /> foreach ($v as $tag=>$val)<br /> {<br /> $n_tf = fopen($n_dir.'/'.$tag.'.txt', 'a+');<br /> fputs($n_tf,implode("\n",$val)."\n");<br /> }<br /> <br /> <br /> }<br /> unset($text_arr);<br /> <br /> ++$i;<br /> <br /> }<br /> <br /> echo microtime(true)-$st;<br /> <br /> ?><br />
<br /> <?php<br /> <br /> // 排序完成拉 (800~920秒)<br /> <br /> set_time_limit(0);<br /> $st = microtime(true);<br /> <br /> $qq_done = fopen('qq_done.txt', 'a+');<br /> <br /> $root = 'qq_no';<br /> $dir_array = scandir($root);<br /> <br /> foreach ($dir_array as $key=>$val)<br /> {<br /> if ($val != '.' && $val != '..')<br /> $dirs[$val] = scandir($root.'/'.$val);<br /> }<br /> <br /> <br /> foreach ($dirs as $key=>$val)<br /> {<br /> foreach ($val as $v)<br /> {<br /> if ($v != '.' && $v != '..')<br /> {<br /> $file = $root. '/' . $key . '/'. $v;<br /> $c = file_get_contents($file);<br /> $arr = explode("\n", $c);<br /> sort($arr);<br /> fputs($qq_done, implode("\n",$arr));<br /> unlink($file);<br /> }<br /> }<br /> rmdir($root. '/' . $key);<br /> }<br /> rmdir($root);<br /> <br /> echo microtime(true)-$st;<br /> <br /> ?><br />
<br> #include <stdio.h><br> <br> #define BITSPERWORD 32<br> #define SHIFT 5<br> #define MASK 0x1F<br> #define N 100000000<br> <br> int a[1 + N/BITSPERWORD];<br> <br> void set(int i) <div class="clear"> </div></stdio.h>