Skip to content

Instantly share code, notes, and snippets.

@yohgaki yohgaki/bench.php Secret
Last active Aug 29, 2015

Embed
What would you like to do?
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
You can’t perform that action at this time.