Created
December 10, 2015 14:44
-
-
Save vgarvardt/1060bbcd48a6eab5c364 to your computer and use it in GitHub Desktop.
Generate unique IDs from microtime and calculate collisions
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 | |
$iterations = 100000; | |
$orderNrLen = 6; | |
for ($secondsLen = 0; $secondsLen < 6; $secondsLen++) { | |
$numbers = []; | |
$collisions = 0; | |
for ($i = 0; $i < $iterations; $i++) { | |
if ($i % 1000 == 0) sleep(1); | |
list($micro, $seconds) = explode(' ', microtime(false)); | |
$micro = strval(intval($micro * 10000000000)); | |
$nr = substr($seconds, -$secondsLen) . substr($micro, 0, $orderNrLen = $secondsLen); | |
if (!empty($numbers[$nr])) { | |
$collisions++; | |
} else { | |
$numbers[$nr] = $nr; | |
} | |
} | |
echo sprintf("Seconds: %d; collisions: %d [ %.4f%% ]\n", $secondsLen, $collisions, $collisions / $iterations); | |
flush(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment