php随机函数mt_rand()和rand()性能对比
在php中mt_rand()和rand()函数都是可以随机生成一个纯数字的,他们都是需要我设置好种子数据然后生成,那么mt_rand()和rand()那个性能会好一些呢,下面我带着疑问来测试一下。
例子 1. mt_rand() 范例
代码如下 | 复制代码 |
echo mt_rand() . "n"; echo mt_rand(5, 15); 上例的输出类似于: 1604716014 |
注: 自 PHP 4.2.0 起,不再需要用 srand() 或 mt_srand() 函数给随机数发生器播种,现已自动完成。
注: 在 3.0.7 之前的版本中,max 的含义是 range。要在这些版本中得到和上例相同 5 到 15 的随机数,简短的例子是 mt_rand (5, 11)。
参见 mt_srand(),mt_getrandmax() 和 rand()。
rand() 函数返回随机整数。
语法
rand(min,max)参数 描述
min,max 可选。规定随机数产生的范围。
说明
如果没有提供可选参数 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()对比测试一
测试代码:
代码如下 | 复制代码 |
$max = 100000; $timeparts = explode(' ',microtime()); |
结果:
第一次测试
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
代码如下 | 复制代码 |
function microtime_float()
|
结果:
第一次
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,所以现在它们速度上没有太大的区别
从上面的各种测试来看它们之间并没有区别,只是在不同系统中可能数值会有变化了。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











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 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 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 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.

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 uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.
