PHP: rand() vs. mt_rand()
The documentation for mt_rand() says that it produces numbers four times faster than the standard rand(). So, being that I use random numbers in my code often, I decided to test this.
Note: There is a followup to this article that discusses the randomness of the numbers generated by the two functions on different operating systems.
The test code:
<?php
$max = 100000;
$timeparts = explode(' ',microtime());
$stime = $timeparts[1].substr($timeparts[0],1);
$i = 0;
while($i < $max) {
rand();
$i++;
}
$timeparts = explode(' ',microtime());
$etime = $timeparts[1].substr($timeparts[0],1);
$time = $etime-$stime;
echo "{$max} random numbers generated in {$time} seconds using rand();<br/>";
$timeparts = explode(' ',microtime());
$stime = $timeparts[1].substr($timeparts[0],1);
$i = 0;
while($i < $max) {
mt_rand();
$i++;
}
$timeparts = explode(' ',microtime());
$etime = $timeparts[1].substr($timeparts[0],1);
$time = $etime-$stime;
echo "{$max} random numbers generated in {$time} seconds using mt_rand();<br/>";
?>
The Results:
On my laptop running a slightly outdated version of PHP 4.4 (around 4.4.0 if I remember correctly), the results were not as I expected. Most of the time, rand() was faster than mt_rand() by about 0.02 seconds. Of course, this isn't a big performance difference, but it's not the four times faster that was promised.
Sample:
100000 random numbers generated in 0.127249002457 seconds using rand();
100000 random numbers generated in 0.140385866165 seconds using mt_rand();
On TechnoServ, the results were closer. Usually rand() won by 0.01 seconds or less. Once again, not the supposed "four times faster."
Sample:
100000 random numbers generated in 0.11243796348572 seconds using rand();
100000 random numbers generated in 0.11861705780029 seconds using mt_rand();
So, despite the PHP documentation, rand() and mt_rand() are about the same speed, rand is just slightly faster.
Comments
nice information.
By pete 06/21/08 03:46:12
I tested it at my laptop with latest php-cli ( thats in the Ubuntu repository ):
10000000 random numbers generated in 3.07709908485 seconds using rand();
10000000 random numbers generated in 2.68754291534 seconds using mt_rand();
Observe that I tested with 100 times more of random numbers :)
10000000 random numbers generated in 3.07709908485 seconds using rand();
10000000 random numbers generated in 2.68754291534 seconds using mt_rand();
Observe that I tested with 100 times more of random numbers :)
By Frank 12/30/09 03:42:48
I got these results while trying on WIN7
100000 random numbers generated in 0.062403202056885 seconds using rand();
100000 random numbers generated in 0.028571844100952 seconds using mt_rand();
It shows that mt_rand(); is 4 times faster.
100000 random numbers generated in 0.062403202056885 seconds using rand();
100000 random numbers generated in 0.028571844100952 seconds using mt_rand();
It shows that mt_rand(); is 4 times faster.
By Xerrion 01/22/10 11:02:44

Since 2004 or 2003, don't remember, the algorythm for rand() has been substituted and now there are no much difference in speed ;)