Skip to content

Instantly share code, notes, and snippets.

@kpacha
Created December 6, 2013 22:04
Show Gist options
  • Save kpacha/7832881 to your computer and use it in GitHub Desktop.
Save kpacha/7832881 to your computer and use it in GitHub Desktop.
Simple php hashing benchmarking (based on a comment from http://www.php.net/manual/en/function.hash.php)
<?php
$totalIterations = 100000;
$data = "test";
$dataToHash = array();
echo "PHP Hash benchmark.\nCreating $totalIterations random strings.\n";
function generateRandomString(
$length = 20, $chars = '-0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
)
{
return substr(str_shuffle($chars), 0, $length);
}
for ($i = 0; $i < $totalIterations; $i++) {
$dataToHash[] = generateRandomString();
}
echo "Done.\n\nHashing $totalIterations pre-generated random strings per algorithm.\n";
printf("%-12s | %s | %-8s | %s\n", 'Algorithm', 'Size', 'Time (s)', "Sample hash ('test')");
echo "-------------+------+----------+----------------------------------\n";
foreach (hash_algos() as $v) {
$testStart = microtime(true);
foreach($dataToHash as $string) {
hash($v, $string, false);
}
$time = microtime(true) - $testStart;
$r = hash($v, $data, false);
printf("%-12s | %3d | %5f | %s\n", $v, strlen($r), $time, $r);
}
echo "Done.\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment