Last active
February 10, 2022 07:37
-
-
Save masakielastic/0f71d119911225b708bc to your computer and use it in GitHub Desktop.
random_int vs mt_rand
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function timer(callable $block) { | |
$start = microtime(true); | |
for ($i = 0; $i < 100000; ++$i) { | |
$block(); | |
} | |
$end = microtime(true); | |
return $end - $start; | |
} | |
$tests = [ | |
'random_int' => timer(function() { | |
random_int(0, 255); | |
}), | |
'mt_rand' => timer(function() { | |
mt_rand(0, 255); | |
}) | |
]; | |
asort($tests); | |
var_dump($tests); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
array(2) { | |
["mt_rand"]=> | |
float(0.017313003540039) | |
["random_int"]=> | |
float(0.028265953063965) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Exactly what I was looking for. Thanks!