#25007 [Opn->Asn]: rand & mt_rand seed RNG every call
| From: | iliaa@php.net | Date: | Sun, 10 Aug 2003 23:56:17 +0000 |
| Subject: | #25007 [Opn->Asn]: rand & mt_rand seed RNG every call | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-45673@lists.php.net to get a copy of this message | ||
ID: 25007
Updated by: iliaa@php.net
Reported By: dcowgill at mail dot communityconnect dot com
-Status: Open
+Status: Assigned
Bug Type: Performance problem
Operating System: Linux
PHP Version: 4.3.3RC3
-Assigned To:
+Assigned To: iliaa
Previous Comments:
------------------------------------------------------------------------
[2003-08-10 18:20:07] dcowgill at mail dot communityconnect dot com
Description:
------------
From the docs for rand: "In older versions of PHP, you
had to seed the random number generator before use with
srand(). Since 4.2.0 this is no longer necessary."
The implication is that rand seeds the RNG the first
time it is called, unless srand has been previously
called; however, rand seeds the RNG every time (or
until srand is explicitly called). This results in a
significant increase in running time (about an order of
magnitude).
The above also applies to mt_rand, except that the
performance penalty is greater (apparently because its
seed-generation algorithm is more expensive).
(the patch to ext/standard/rand.c to correct the
problem is trivial)
Reproduce code:
---------------
// t1.php
<?
for ($i=0; $i<100000; $i++) rand(0,10);
?>
// t2.php
<?
srand();
for ($i=0; $i<100000; $i++) rand(0,10);
?>
Actual result:
--------------
$ time php t1.php
real 0m0.729s
user 0m0.650s
sys 0m0.080s
$ time php t2.php
real 0m0.101s
user 0m0.100s
sys 0m0.000s
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=25007&edit=1