Skip to content

Instantly share code, notes, and snippets.

@borisguery
Created September 28, 2012 09:01
Show Gist options
  • Save borisguery/3798757 to your computer and use it in GitHub Desktop.
Save borisguery/3798757 to your computer and use it in GitHub Desktop.
GUID generation benchmark
<?php
$options = getopt('i:c:');
if (!isset($options['i']) && !isset($options['c'])) {
printf("Usage: %s -i iterations -c count\n", ltrim($argv[0], './'));
exit(0);
}
$iterations = $options['i'];
$count = $options['c'];
$starts = array();
$ends = array();
for ($i = 1; $i <= $iterations; ++$i) {
$starts[$i] = microtime(true);
printf("Iteration #%d\n", $i);
for ($j = 1; $j <= $count; ++$j) {
printf("\r%d/%d (%2.f%%)", $j, $count, ($j / $count * 100));
$guid = sprintf(
'%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff),
mt_rand(0, 0x0fff) | 0x4000,
mt_rand(0, 0x3fff) | 0x8000,
mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
);
}
$ends[$i] = microtime(true);
printf("\n");
}
printf("\n--------------------\n");
printf("Results: \n");
$elapsed = array();
for ($k = 1; $k <= count($ends); ++$k) {
$elapsed[] = $ends[$k] - $starts[$k];
printf("Iteration #%d tooks %f seconds\n", $k, end($elapsed));
}
printf("\nBest time: %f seconds\n", min($elapsed));
printf("Worst time: %f seconds\n", max($elapsed));
printf("Average time: %f seconds\n\n", array_sum($elapsed) / count($elapsed));
exit(1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment