Skip to content

Instantly share code, notes, and snippets.

@padakuro
Created February 4, 2014 06:39
Show Gist options
  • Select an option

  • Save padakuro/8799106 to your computer and use it in GitHub Desktop.

Select an option

Save padakuro/8799106 to your computer and use it in GitHub Desktop.
php hash_file benchmark (requires dd for temp file generation)
<?php
// will be passed to dd as bs=
$sizes = ['1K', '10K', '100K', '1M', '10M', '25M', '50M', '100M'];
// http://de3.php.net/manual/en/function.hash-algos.php
$algos = ['md5', 'sha256', 'sha512', 'ripemd320', 'whirlpool'];
echo "After test, please remove all *.hashtest temporary files in this directory\n";
echo "Preparing temporary files...\n";
// prepare temp files
foreach($sizes as $size) {
$file = sprintf('%s.hashtest', $size);
if(!file_exists($file)) {
exec(sprintf('dd if=/dev/urandom of=%s count=1 bs=%s', $file, $size));
}
}
// run test
echo "Running tests...\n";
$result = [];
foreach($algos as $algo) {
printf("%s\n", $algo);
$result[$algo] = [];
foreach($sizes as $size) {
$file = sprintf('%s.hashtest', $size);
printf(" > %4s: ", $size);
$t = microtime(true);
hash_file($algo, $file);
$t = microtime(true) - $t;
$result[$algo][$size] = sprintf('%f', $t);
printf("%f\n", $t);
}
}
// CSV result
echo "\n\n";
printf("algo;%s\n", implode(';', $sizes));
foreach($result as $algo => $times) {
printf("%s;%s\n", $algo, implode(';', $result[$algo]));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment