Home  >  Article  >  Backend Development  >  PHP generates millions of UIDs and stores them in files

PHP generates millions of UIDs and stores them in files

WBOY
WBOYOriginal
2016-08-08 09:20:201066browse

This is a question asked in a group today. The main reason is that writing files takes a long time.

I am doing an experiment here. Every 20,000 UIDs are written into a file. One file is written approximately 280kb, so the overall writing time is only 13S

Post the code


set_time_limit(0);

$starttime = time();

$fh = fopen($filename,'w');

for($i=0;$i<1000000;$i++)
{
    $uid = uniqid();
    if($i%20000 == 0) 
    {
        $filename = $i.'.txt';

    }
    fwrite($fh,$uid.' ');
}
$endtime = time();
echo'总用时:'.($endtime-$starttime).'s';
?>

Copyright statement: This article is a blog This is an original article and may not be reproduced without the permission of the blogger.

The above introduces PHP to generate millions of UIDs and save them in files, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn