note 67516 added to function.rand
| From: | popefelix at gmail dot com | Date: | Thu, 08 Jun 2006 14:25:08 +0000 |
| Subject: | note 67516 added to function.rand | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-113839@lists.php.net to get a copy of this message | ||
A somewhat cursory test suggests that the random string generator
provided by "thebomb-hq" is faster than that provided by
"RichardBronosky". On my laptop (1.3 GHz Pentium-M, PHP 5.1.4,
kernel 2.6.16-gentoo-r4):
time elapsed in creating array of 1000000 random 10-character strings
via randomkeys() (thebomb): 27.8403680325s
2.78403680325E-05s per string
2.78403680325E-06s per character
time elapsed in creating array of 1000000 random 10-character strings
via randomkeys2() (Bronosky): 42.8195331097s
4.28195331097E-05s per string
4.28195331097E-06s per character
Test code:
<?php
$nkeys = 1000000;
$strlen = 10;
$time0 = getmicrotime();
$strkeysArr = array();
for ($i = 0; $i < $nkeys; $i++) {
$strkey = randomkeys($strlen);
$strkeysArr[] = $strkey;
}
$time1 = getmicrotime();
print "time elapsed in creating array of $nkeys random $strlen-character "
. "strings via randomkeys() (thebomb): " . ($time1 - $time0) . "s\\n";
print ($time1 - $time0)/$nkeys . "s per string\\n";
print (($time1 - $time0)/$nkeys)/$strlen . "s per character\\n";
$time0 = getmicrotime();
$strkeysArr = array();
for ($i = 0; $i < $nkeys; $i++) {
$strkey = randomkeys2($strlen);
$strkeysArr[] = $strkey;
}
$time1 = getmicrotime();
print "time elapsed in creating array of $nkeys random $strlen-character "
. "strings via randomkeys2() (Bronosky): " . ($time1 - $time0) . "s\\n";
print ($time1 - $time0)/$nkeys . "s per string\\n";
print (($time1 - $time0)/$nkeys)/$strlen . "s per character\\n";
// yanked from http://www.php.net/manual/en/function.rand.php
// written by thebomb-hq [AT] gmx [DOT] de
function randomkeys($length) {
$pattern = "1234567890abcdefghijklmnopqrstuvwxyz";
for ($i = 0; $i < $length; $i++) {
$key .= $pattern{rand(0,35)};
}
return $key;
}
// yanked from http://www.php.net/manual/en/function.rand.php
// written by RichardBronoksy
function randomkeys2($length) {
for ($x = 0; $x < $length; $x++){
$y = rand(0,61);
$z .= chr( $y + (($y<10) ? 48 : (($y<36) ? 55 : 61)) );
}
return $z;
}
// Yanked from microtime() manual page on php.net
function getmicrotime() {
list($usec, $sec) = explode(" ",microtime());
return ((float)$usec + (float)$sec);
}
?>
----
Server IP: 66.163.161.117
Probable Submitter: 69.38.90.93
----
X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER
autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/function.rand.php
Edit -- http://master.php.net/note/edit/67516
Del: integrated -- http://master.php.net/note/delete/67516/integrated
Del: useless -- http://master.php.net/note/delete/67516/useless
Del: bad code -- http://master.php.net/note/delete/67516/bad+code
Del: spam -- http://master.php.net/note/delete/67516/spam
Del: non-english -- http://master.php.net/note/delete/67516/non-english
Del: in docs -- http://master.php.net/note/delete/67516/in+docs
Del: other reasons-- http://master.php.net/note/delete/67516
Reject -- http://master.php.net/note/reject/67516
Search -- http://master.php.net/manage/user-notes.php