php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #37106 uniqid() without more_entropy extremally slow
Submitted: 2006-04-17 14:52 UTC Modified: 2006-05-28 12:34 UTC
From: webmaster at polskabizuteria dot pl Assigned:
Status: Not a bug Package: Performance problem
PHP Version: * OS: *
Private report: No CVE-ID: None
 [2006-04-17 14:52 UTC] webmaster at polskabizuteria dot pl
Description:
------------
Uniquid() without second parameter (more_entropy) set as true is extremaly slow (approx 500 times)

Reproduce code:
---------------
/* SLOW */

uniqid();
uniqid("");
uniqid("",false);

/* FAST */

uniqid("",true);


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-05-28 12:34 UTC] helly@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

uniqid() is meant to give you a certrain id distribuion that is not predicable. That is uniqid() must neither generate two identical ids nor have a constant or in any way predictable difference between two calls. To ensure this we use usleep() internally to either force a thread (windows) or process (*nix) switch. On some systems the operating system has a slow implementation of this. However distrubition this much more important than the security risks implied.
 [2013-10-10 15:18 UTC] scott dot baker at gmail dot com
$loop = 10000;

print "uniqid('') x $loop = " . bench('gen_uniqid',$loop) . " seconds<br />";
print "uniqid('',true) x $loop = " . bench('gen_uniqid_entropy',$loop) . " seconds<br />";

function gen_uniqid() {
   return uniqid('');
}

function gen_uniqid_entropy() {
   return uniqid('',true);
}

function bench($func,$times) {
   $start = microtime(1);

   for ($i = 0; $i < $times; $i++) {
      call_user_func($func);
   }

   $end = microtime(1);
   $ret = sprintf("%0.4f",$end - $start);

   return $ret;
}

------------------------------------------------------

uniqid('') x 10000 = 1.5876 seconds
uniqid('',true) x 10000 = 0.0329 seconds

Telling uniqid() to use more_entropy is MORE than 48 times faster
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 07 22:01:27 2024 UTC