Last active
August 29, 2015 14:23
-
-
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)
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; | |
} | |
$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); |
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(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