Skip to content

Instantly share code, notes, and snippets.

@masakielastic
Last active February 10, 2022 07:37
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save masakielastic/0f71d119911225b708bc to your computer and use it in GitHub Desktop.
Save masakielastic/0f71d119911225b708bc to your computer and use it in GitHub Desktop.
random_int vs mt_rand
<?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);
array(2) {
["mt_rand"]=>
float(0.017313003540039)
["random_int"]=>
float(0.028265953063965)
}
@harryqt
Copy link

harryqt commented Feb 10, 2022

Exactly what I was looking for. Thanks!

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