モノノフ日記

普通の日記です

Re: PHPのuniqid()が重い件について

PHPのuniqid()が重い件について - a_a___a_a

試してみたら自分の環境でもuniqidの処理遅いので調べてみたら、(4年前に)バグとして報告されていました。

At last, I profiled (APD) it and discovered that uniqid() was terribly slow.I searched for some big problem in my app then, without any other clue,I commented out the usleep() call in uniqid.c: magically, a page that took 10 seconds now responded under 1 sec!

PHP :: Bug #37840 :: uniqid() without more_entropy extremely slow

とりあえずの解決方法としては第2引数のmore_entropyをtrueにしたらOKです。比較用に引数指定ありなしでの結果貼っておきます。

uniqid()
$ time php -r 'for($i=0;$i<1000;$i++){uniqid();}'

real    0m20.036s
user    0m0.010s
sys     0m0.020s
uniqid("", true)
$ time php -r 'for($i=0;$i<1000;$i++){uniqid("", true);}'

real    0m0.045s
user    0m0.020s
sys     0m0.020s

more_entropyをtrueにすると、falseとは返り値が変わってくるのでそこは留意してください。
uniqidの実装コードまでは時間無くて読めてない(><)