Mt_rand of random functions in PHP () and Rand () performance comparison and analysis _php skills

Source: Internet
Author: User
Tags explode generator rand

This paper compares and analyzes the performance problem of random function Mt_rand () and Rand () in PHP. Share to everyone for your reference. The specific analysis is as follows:

The Mt_rand () and Rand () functions in PHP can randomly generate a pure number, they all need us to set up the seed data and then generate, then Mt_rand () and Rand () that the performance will be better, the following we have a question to test.

Example 1. Mt_rand () example, the code is as follows:

Copy Code code as follows:
<?php
Echo Mt_rand (). "N";
Echo Mt_rand (). "N";
Echo Mt_rand (5, 15);
?>

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

1604716014
1478613278
6

Note: since PHP 4.2.0, no longer need to use the Srand () or Mt_srand () function for the random number generator seeding, has been automatically completed.

Note: In the previous version of 3.0.7, Max means range, in which case the random number of the same 5 to 15 in the above example is given, and the short example is Mt_rand (5, one).

For more information, refer to Mt_srand (), Mt_getrandmax (), and Rand () related documents.

The rand () function returns a random integer.

Syntax: rand (MIN,MAX)

Parameters Describe
Min,max Optionally, specify the range that the random number produces.

Note: If you do not provide optional parameters min and Max,rand () return a pseudo-random integer between 0 and Rand_max, for example, want a random number between 5 and 15 (including 5 and 15), with Rand (5, 15).

Tips and comments

Note: Under some platforms (for example, Windows) Rand_max is only 32768, and if the desired 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, you no longer need to use the Srand () or Mt_srand () function to sow the random number generator, which is now automatically completed.

Note: In the previous version of 3.0.7, Max means range, where you get the same random number as the previous example of 5 to 15, and the short example is rand (5, one).

is Mt_rand () really 4 times times faster than Rand ()? Take this question and test yourself while you are on the Web. The test is as follows.

Mt_rand () and rand () contrast test one, the test code is as follows:

Copy Code code as follows:
<?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 ();";

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

The test results are as follows:
First time Test
100000 random numbers generated in 0.024894952774048 using rand ();
100000 random numbers generated in 0.028925895690918 using seconds ();
Second Test
100000 random numbers generated in 0.03147292137146 using rand ();
100000 random numbers generated in 0.02997088432312 using seconds ();
Third time Test
100000 random numbers generated in 0.028102874755859 using rand ();
100000 random numbers generated in 0.02803111076355 using seconds ();
Fourth time Test
100000 random numbers generated in 0.025573015213013 using rand ();
100000 random numbers generated in 0.028030157089233 using seconds ();

This result is only a few times show results, more than a few times 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

The code is as follows:

Copy Code code as follows:
<?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";
?>

The test results are as follows:
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 is alternating, but there is not much difference between the two.

PHP's Mt_rand () and Rand () contrast conclusion

Read a lot of other people's tests on the Internet, with Linux and the Windows environment, most people come up with the same results as mine: The two are about the same, but others have detected that Mt_rand () is 4 times times faster than Rand (), but because they didn't give a specific test environment, they couldn't tell if it was true or false. I'm still more convinced of my conclusion because I saw someone introduce 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, so 10 years ago, and Rand () the difference in Speed is (4 times times), since 2004, Rand () has started to use Algorythm, So now they don't have much of a difference in speed.

From the various tests above, there is no difference between them, but the values may change in different systems.

I hope this article will help you with your PHP program design.

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.