Skip to content

Instantly share code, notes, and snippets.

@trgino
Created November 15, 2022 19:30
Show Gist options
  • Save trgino/3a97b7223b206b2c38fdd9cf013968df to your computer and use it in GitHub Desktop.
Save trgino/3a97b7223b206b2c38fdd9cf013968df to your computer and use it in GitHub Desktop.
php rand vs mt_rand vs random_int fastest benchmark
<?php
$a1=microtime(true);
foreach(range(1,10) as $a){
echo mt_rand(0,100).PHP_EOL;
}
echo ':'.microtime(true)-$a1.PHP_EOL;
$a1=microtime(true);
foreach(range(1,10) as $a){
echo random_int(0,100).PHP_EOL;
}
echo ':'.microtime(true)-$a1.PHP_EOL;
$a1=microtime(true);
foreach(range(1,10) as $a){
echo rand(0,100).PHP_EOL;
}
echo ':'.microtime(true)-$a1.PHP_EOL;
@trgino
Copy link
Author

trgino commented Nov 15, 2022

59
17
25
95
70
51
15
73
43
70
:6.6995620727539E-5
92
43
4
0
98
4
76
85
79
23
:3.0040740966797E-5
11
47
52
35
43
69
52
54
18
86
:4.4107437133789E-5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment