Skip to content

Instantly share code, notes, and snippets.

@masakielastic
Last active August 29, 2015 14:23
Show Gist options
  • Save masakielastic/76ec50728d259626cf14 to your computer and use it in GitHub Desktop.
Save masakielastic/76ec50728d259626cf14 to your computer and use it in GitHub Desktop.
PRNG Benchmarks (derived from the following code: https://gist.github.com/sarciszewski/f7bd4c0358a44321787b)
<?php
function timer(callable $block) {
$start = microtime(true);
for ($i = 0; $i < 100000; ++$i) {
$block();
}
$end = microtime(true);
return $end - $start;
}
$buf = '';
$tests = [
'random_bytes' => timer(function() {
$buf = random_bytes(32);
}),
'openssl_random_pseudo_bytes' => timer(function() {
$buf = openssl_random_pseudo_bytes(32);
}),
'mcrypt_create_iv' => timer(function() {
$buf = mcrypt_create_iv(32, MCRYPT_DEV_URANDOM);
}),
'mt_rand' => timer(function() {
$buf = '';
for ($i = 0; $i < 32; ++$i) {
$buf .= chr(mt_rand(0, 255));
}
})
];
var_dump($tests);
array(4) {
["random_bytes"]=>
float(0.038606882095337)
["openssl_random_pseudo_bytes"]=>
float(0.26424193382263)
["mcrypt_create_iv"]=>
float(0.42767190933228)
["mt_rand"]=>
float(0.79697513580322)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment