note 128573 added to function.mt-rand
| From: | MarioM dot Junior at main dot php dot net | Date: | Fri, 26 May 2023 22:18:24 +0000 |
| Subject: | note 128573 added to function.mt-rand | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-220528@lists.php.net to get a copy of this message | ||
According to my simple benchmark below, mt_rand() is slightly faster than rand(), while random_int()
is the slowest by far.
Results:
Testing with rand()...
0.14806389808655
----- -----
Testing with mt_rand()...
0.12115501403809
----- -----
Testing with random_int()...
2.4194748401642
Benchmark code:
echo date("H:i:s") . "\n";
echo "----- -----\n";
$t = microtime(true) . "\n";
echo "Testing with rand()...\n";
for ($i = 1; $i <= 2000000; ++$i) {
rand(100000, 500000);
}
echo microtime(true) - $t . "\n";
echo "----- -----\n";
$t = microtime(true) . "\n";
echo "Testing with mt_rand()...\n";
for ($i = 1; $i <= 2000000; ++$i) {
mt_rand(100000, 500000);
}
echo microtime(true) - $t . "\n";
echo "----- -----\n";
$t = microtime(true) . "\n";
echo "Testing with random_int()...\n";
for ($i = 1; $i <= 2000000; ++$i) {
random_int(100000, 500000);
}
echo microtime(true) - $t . "\n";
echo date("H:i:s") . "\n";
----
Server IP: 45.112.84.4
Probable Submitter: 80.90.13.130 (proxied: 191.184.0.142)
----
Manual Page -- https://php.net/manual/en/function.mt-rand.php
Edit -- https://main.php.net/note/edit/128573
Del: integrated -- https://main.php.net/note/delete/128573/integrated
Del: useless -- https://main.php.net/note/delete/128573/useless
Del: bad code -- https://main.php.net/note/delete/128573/bad+code
Del: spam -- https://main.php.net/note/delete/128573/spam
Del: non-english -- https://main.php.net/note/delete/128573/non-english
Del: in docs -- https://main.php.net/note/delete/128573/in+docs
Del: other reasons-- https://main.php.net/note/delete/128573
Reject -- https://main.php.net/note/reject/128573
Search -- https://main.php.net/manage/user-notes.php