PHP random function Mt_rand () and Rand () performance comparison _php tutorial

Source: Internet
Author: User
in PHP Mt_rand () and Rand () functions are randomly generated a pure number, they all need me to set up the seed data and then generate, then Mt_rand () and Rand () that performance will be better, the following I have questions to test.

Example 1. Mt_rand () example

The code is as follows Copy Code

Echo Mt_rand (). "N";
Echo Mt_rand (). "N";

Echo Mt_rand (5, 15);
?>

The output of the previous example is similar to the following:

1604716014
1478613278
6

Note: Since PHP 4.2.0, it is no longer necessary to use the Srand () or Mt_srand () function to sow the random number generator, which is now done automatically.

Note: In the previous version of 3.0.7, Max means range. To get the same random number from the previous example of 5 to 15 in these versions, the short example is Mt_rand (5, 11).

See Mt_srand (), Mt_getrandmax (), and Rand ().


The rand () function returns a random integer.

Grammar

RAND (Min,max) parameter description
Min,max is optional. Specifies the range of random numbers produced.

Description

If no optional parameter min and Max,rand () are provided, the pseudo-random integer between 0 and Rand_max is returned. For example, want a random number between 5 and 15 (including 5 and 15), with Rand (5, 15).
Hints and Notes
Note: Under some platforms (for example, Windows) Rand_max is only 32768. If the required range is greater than 32768, then specifying the min and Max parameters can generate a number greater than Rand_max, or consider replacing it with Mt_rand ().

Note: Since PHP 4.2.0, it is no longer necessary to sow the random number generator with the Srand () or Mt_srand () function, which is now done automatically.

Note: In the previous version of 3.0.7, Max means range. To get the same random number from the previous example of 5 to 15 in these versions, the short example is rand (5, 11).


is Mt_rand () really going to be 4 times times faster than Rand ()?

With this question, one side of the test while watching the online introduction, the test is as follows.

Mt_rand () and rand () contrast test one

Test code:

The code is as follows Copy Code

$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 ();
";

$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 ();
";
?>

Results:

First time Test
100000 random numbers generated in 0.024894952774048 seconds using rand ();
100000 random numbers generated in 0.028925895690918 seconds using Mt_rand ();
Second Test
100000 random numbers generated in 0.03147292137146 seconds using rand ();
100000 random numbers generated in 0.02997088432312 seconds using Mt_rand ();
Third-time Test
100000 random numbers generated in 0.028102874755859 seconds using rand ();
100000 random numbers generated in 0.02803111076355 seconds using Mt_rand ();
Fourth time Test
100000 random numbers generated in 0.025573015213013 seconds using rand ();
100000 random numbers generated in 0.028030157089233 seconds using Mt_rand ();

This result is only a few times to show the results, multiple tests you will find that the two are alternating, in fact, there is not much difference between the two.

Mt_rand () and rand () contrast test two

I test the environment
Operating system: Windows XP
Apache 2.0
PHP 5.2.12
Memory 2G

code as follows copy code
"!--? php
function microtime_float ()
{
List ($usec, $sec) = Explode ("", Microtime ());
return (float) $usec + (float) $sec);
}
$time _start = Microtime_float ();
for ($i =0; $i <1000000; + + $i)
{
rand ();
}
$time _end = Microtime_float ();
$time = $time _end-$time _start;
Echo "rand () cost $time secondsn";


$time _start = Microtime_float ();
for ($i =0; $i <1000000; + + $i)
{
Mt_rand ();
}
$time _end = Microtime_float ();
$time = $time _end-$time _start;
Echo "Mt_rand () cost $time secondsn";
?

Results:
First time
RAND () Cost 0.25919604301453 seconds
Mt_rand () Cost 0.28554391860962 seconds
Second time
RAND () Cost 0.31136202812195 seconds
Mt_rand () Cost 0.28973197937012 seconds
Third time
RAND () Cost 0.27545690536499 seconds
Mt_rand () Cost 0.27108001708984 seconds
four times
RAND () Cost 0.26263308525085 seconds
Mt_rand () Cost 0.27727103233337 seconds

The result is the same: the time used is alternating, but there is not much difference between the two.
PHP's Mt_rand () vs rand () conclusion

Watching a lot of other people's tests on the Internet, Linux and the Windows environment, most people came up with the same results as mine: But some people measured mt_rand () 4 times times faster than Rand (), but because they did not give a specific test environment, so can not judge the true and false. I still believe my conclusion because I see someone introducing Mt_rand () with Rand ():


So why does the PHP manual say that Mt_rand () is 4 times times faster than Rand ()?

This is because Mt_rand () uses the Mersenne Twister Algorythm is 1997 things, so 10 years ago, and Rand () the difference in Speed is (4 times times).
Since 2004, Rand () has started using algorythm, so now they don't have much difference in speed.

There is no difference between them from the various tests above, except that the values may change in different systems.


http://www.bkjia.com/PHPjc/445615.html www.bkjia.com true http://www.bkjia.com/PHPjc/445615.html techarticle in PHP the Mt_rand () and Rand () functions are randomly generated in a pure number, they all need me to set up the seed data and then generate, then Mt_rand () and Rand () that performance will be better ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.