Home >Backend Development >PHP Tutorial >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:201181browse

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

<code><span><span><?php</span>
set_time_limit(<span>0</span>);

<span>$starttime</span> = time();

<span>$fh</span> = fopen(<span>$filename</span>,<span>'w'</span>);

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

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

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