note 67516 deleted from function.rand by bjori

From: Date: Thu, 08 Jun 2006 16:59:02 +0000
Subject: note 67516 deleted from function.rand by bjori
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-113865@lists.php.net to get a copy of this message
Note Submitter: popefelix at gmail dot com Reason: useless ---- 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); } ?>

« previous php.notes (#113865) next »