Skip to content

Instantly share code, notes, and snippets.

@yohgaki
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yohgaki/ede544f290c6cf9fa90d to your computer and use it in GitHub Desktop.
Save yohgaki/ede544f290c6cf9fa90d to your computer and use it in GitHub Desktop.
PHP Script that benchmarks verious timing safe functions. https://github.com/yohgaki/php-src/compare/PHP-5.6-rfc-hash-compare
<?php
$iterations = 1000000;
$data_size = 1024;
$funcs = ['str_siphash_compare', 'str_xxhash32_compare', 'str_md5_compare', 'str_byte_compare', 'str_byte_compare2', 'str_word_compare', 'str_compare' ];
foreach($funcs as $f) {
benchmark($f, $iterations, $data_size);
}
function benchmark($f, $i, $dsz) {
$d1 = file_get_contents("/dev/urandom", 0,NULL,-1,$dsz);
$d2 = file_get_contents("/dev/urandom", 0,NULL,-1,$dsz);
$n = 0;
$start = microtime(TRUE);
while($n < $i) {
if ($f($d1,$d2)) {
echo 'Error: '.$f.' '.$n.$d1.$d2.PHP_EOL;
die('Exiting'.PHP_EOL);
}
$n++;
}
printf('%-20s Elapsed: %f Iterations: %d DataSize: %d'.PHP_EOL, $f, microtime(TRUE)-$start, $i, $dsz);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment