php function Mt_rand () and rand () performance test comparison example

Source: Internet
Author: User
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:

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

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

<?php Echo Mt_rand (). "N"; Echo Mt_rand (). "N"; Echo Mt_rand (5,?>);

The results are as follows:

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 versions of 3.0.7, Max was meant to be a range, with the same random number as in the previous example of 5 to 15, and a short example of Mt_rand (5, one).

Details can be found in Mt_srand (), Mt_getrandmax () and rand () related documents.

The rand () function returns a random integer.

Syntax: rand (MIN,MAX)

Parameters Describe
Min,max Optional, which specifies the range of random numbers produced.

Description: If no optional parameter min and Max,rand () are provided, 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).

Hints and Notes

Note: Under some platforms (for example, Windows) Rand_max is only 32768, if the required range is greater than 32768, the specified 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 was meant to be a range, with the same random number as in the previous example of 5 to 15, and the short example is rand (5, one).

is Mt_rand () really going to be 4 times times faster than Rand ()? Take this question and watch the introduction on the Internet while you test. The test is as follows.

Mt_rand () and rand () compare test one, the test code is 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 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 environment, operating system: Windows Xp,apache 2.0,php 5.2.12, Memory 2G

The code is as follows:

<?php function microtime_float () {list ($usec, $sec) = Explode ("", Microti      Me ());  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 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.


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.