Home Backend Development PHP Tutorial Detailed explanation of performance test comparison examples of php function mt_rand() and rand()

Detailed explanation of performance test comparison examples of php function mt_rand() and rand()

Jun 29, 2017 am 10:47 AM
php rand Compared

The example in this article compares and analyzes the performance issues of random functions mt_rand() and rand() in PHP. Share it with everyone for your reference. The specific analysis is as follows:

In PHP, the mt_rand() and rand() functions can randomly generate a pure number. They both require us to set the seed data and then generate it. Then Which one of mt_rand() and rand() has better performance? Let’s test it with questions.

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

<?php 
echo mt_rand() . "n"; 
echo mt_rand() . "n"; 
echo mt_rand(5, 15); 
?>
Copy after login

The results are as follows:

1604716014 
1478613278 
6
Copy after login

Note:Since PHP 4.2.0, it is no longer necessary to use the srand() or mt_srand() function to seed the random number generator, it is nowcompleted automatically.

Note:In versions before 3.0.7, max means range. To get the same random number from 5 to 15 as in the above example in these versions, a short example is mt_rand (5, 11).

For details, please refer to the relevant documents of mt_srand(), mt_getrandmax() and rand().

rand() function returns a random integer.

Syntax: rand(min,max)

ParametersDescription
min,maxOptional, specify the range of random number generation.

说明:如果没有提供可选参数 min 和 max,rand() 返回 0 到 RAND_MAX 之间的伪随机整数,例如,想要 5 到 15(包括 5 和 15)之间的随机数,用 rand(5, 15).

提示和注释

注释:在某些平台下(例如 Windows)RAND_MAX 只有 32768,如果需要的范围大于 32768,那么指定 min 和 max 参数就可以生成大于 RAND_MAX 的数了,或者考虑用 mt_rand() 来替代它.

注释:自 PHP 4.2.0 起,不再需要用 srand() 或 mt_srand() 函数给随机数发生器播种,现在已自动完成.

注释:在 3.0.7 之前的版本中,max 的含义是 range,要在这些版本中得到和上例相同 5 到 15 的随机数,简短的例子是 rand (5, 11).

mt_rand()真的会比rand()快4倍吗?带着这个疑问一边自己测试一边看网上的介绍.测试如下.

mt_rand()和rand()对比测试一,测试代码如下:

<?php  
$max = 100000;  
$timeparts = explode(&#39; &#39;,microtime());  
$stime = $timeparts[1].substr($timeparts[0],1);  
$i = 0;  
while($i < $max) {  
rand();  
$i++;  
}  
$timeparts = explode(&#39; &#39;,microtime());  
$etime = $timeparts[1].substr($timeparts[0],1);  
$time = $etime-$stime;  
echo "{$max} random numbers generated in {$time} seconds using rand();"; 
 
$timeparts = explode(&#39; &#39;,microtime());  
$stime = $timeparts[1].substr($timeparts[0],1);  
$i = 0;  
while($i < $max) {  
mt_rand();  
$i++;  
}  
$timeparts = explode(&#39; &#39;,microtime());  
$etime = $timeparts[1].substr($timeparts[0],1);  
$time = $etime-$stime;  
echo "{$max} random numbers generated in {$time} seconds using mt_rand(); ";  
?>
Copy after login


测试结果如下:
//第一次测试
100000 random numbers generated in 0.024894952774048 seconds using rand();
100000 random numbers generated in 0.028925895690918 seconds using mt_rand();
//第二次测试
100000 random numbers generated in 0.03147292137146 seconds using rand();
100000 random numbers generated in 0.02997088432312 seconds using mt_rand();
//第三次测试
100000 random numbers generated in 0.028102874755859 seconds using rand();
100000 random numbers generated in 0.02803111076355 seconds using mt_rand();
//第四次测试
100000 random numbers generated in 0.025573015213013 seconds using rand();
100000 random numbers generated in 0.028030157089233 seconds using mt_rand();

这个结果只是几次的显示结果,多测试几次你会发觉,两者是交替变化的,其实两者没有太大的差异.

mt_rand()和rand()对比测试二

本人测试环境,操作系统:windows xp,apache 2.0,php 5.2.12,内存 2G

代码如下:

<?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";  
?>
Copy after login

测试结果如下:
//第一次
rand() cost 0.25919604301453 seconds
mt_rand() cost 0.28554391860962 seconds
//第二次
rand() cost 0.31136202812195 seconds
mt_rand() cost 0.28973197937012 seconds
//第三次
rand() cost 0.27545690536499 seconds
mt_rand() cost 0.27108001708984 seconds
//第四次
rand() cost 0.26263308525085 seconds
mt_rand() cost 0.27727103233337 seconds
结果还是一样:两者用的时间是交替变化,其实两者没有太大的差异.

php的mt_rand()与rand()对比结论

在网上看了很多别人的测试,有linux的还有windows环境的,大多数人得出的结果和我的一样:两者相差无几,不过也有人测出mt_rand()比rand()快4倍,但是由于他们没给出具体的测试环境,所以无法判断真假。我还是比较相信我的结论,因为我看到有人这样介绍mt_rand()与rand():

那为什么php手册上说mt_rand()比rand()快4倍呢?

这是因为mt_rand()使用的Mersenne Twister algorythm是1997的事,所以在10年前,和rand()在速度上的差异是(4倍),自2004年,rand()已经开始使用algorythm,所以现在它们速度上没有太大的区别.

从上面的各种测试来看它们之间并没有区别,只是在不同系统中可能数值会有变化了.


The above is the detailed content of Detailed explanation of performance test comparison examples of php function mt_rand() and rand(). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1253
24
PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

See all articles