Last active
January 4, 2016 03:33
-
-
Save carcinocron/e09ef8b98aa809880c07 to your computer and use it in GitHub Desktop.
This file contains 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
<pre><?php | |
/** | |
The goal is fast + unique, this is not for cryptographically secure purposes! | |
*/ | |
define('LOOP_COUNT',100000); | |
define() | |
$startTime = microtime(true); | |
for($x=0;$x<LOOP_COUNT;$x++) | |
{ | |
// Your content to test | |
$var =mt_rand(100000000000000,999999999999999); | |
} | |
$endTime = microtime(true); | |
$elapsed = $endTime - $startTime; | |
echo 'mt_rand in: ',$elapsed,"\n"; | |
$startTime = microtime(true); | |
for($x=0;$x<LOOP_COUNT;$x++) | |
{ | |
// Your content to test | |
$var = openssl_random_pseudo_bytes(14); | |
} | |
$endTime = microtime(true); | |
$elapsed = $endTime - $startTime; | |
echo 'openssl_random_pseudo_bytes(14): in ',$elapsed,"\n"; | |
$startTime = microtime(true); | |
for($x=0;$x< LOOP_COUNT;$x++) | |
{ | |
// Your content to test | |
$var = uniqid('',true); | |
} | |
$endTime = microtime(true); | |
$elapsed = $endTime - $startTime; | |
echo 'uniqid(\'\',true) in: ',$elapsed,"\n"; | |
$startTime = microtime(true); | |
for($x=0;$x< LOOP_COUNT;$x++) | |
{ | |
// Your content to test | |
$var = uniqid('',false); | |
} | |
$endTime = microtime(true); | |
$elapsed = $endTime - $startTime; | |
echo 'uniqid(\'\',false) in: ',$elapsed,"\n"; | |
?> |
uniqid
calls getdatetime()
, but uniqid('',false)
does extra blocking to the CPU and uses sleep(1)
to guarantee that no other process gets the same value. The extra entropy flag seems to be free or built in, we're just halting and blocking the CPU to ensure that a shorter value is unique, which is a total waist.
my laptop, single threaded:
mt_rand in: 0.036395788192749
openssl_random_pseudo_bytes(14): in 0.10478019714355
uniqid('',true) in: 0.050448894500732
uniqid('',false) in: 47.480038881302
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
single threaded assumption results using phpfiddle:
I expect
uniqid('',false)
to perform worse in a multi-php-fpm ecosystem. This benchmark shows high performance but does not show high availability (todo!!!)