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 />
稍等一两分钟1亿个随机QQ创建完成了。
QQ号码范围为>10000。文件大小大概有840MB。
下面就进行分类划分成几千份文件。
以QQ号码长度为文件夹,QQ号码前3位为文件名。
<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 />
总共大概花费了20多分钟。
虽然完成了,但方法很土鳖 0_0 ,坛里各位高手们改进改进啊。
------解决方案--------------------
来个C版本的
<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>

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Notepad++7.3.1
Easy-to-use and free code editor

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.
