我想知道每个散列算法在给定不同数据集长度的特定系统上需要多长时间
hash
的PHP.net页面在注释中有一些示例代码,演示如何分析各种PHP散列函数。
这是一个人记录的时间,以及一些关于如何复制测试的示例代码。
我快速编写了这个脚本。我建议使用$random_len值。不同的值会产生一些有趣的结果。
<?php
$random_len = 100; /* bytes */
$time_begin = microtime(true);
$table_html = '';
$algos = hash_algos();
$fh = @fopen('/dev/urandom', 'rb');
$random = fread( $fh, $random_len );
$time_rand = microtime(true) - $time_begin;
foreach ($algos as $algo) {
$begin = microtime(true);
$hash = hash($algo, $random);
$end = microtime(true) - $begin;
$table_html .= '<tr><td>' . $algo . '</td><td>' . $end . '</td><td>' . $hash . '</td></tr>';
}
$time_end = microtime(true) - $time_begin;
?>
<html>
<style>body{font-family:monospace}td{white-space:nowrap}</style>
<h1>PHP hashing algorithm execution time</h1>
<p>Random data length: <?php echo $random_len; ?> bytes</p>
<p>Random data elapsed time: <?php echo $time_rand; ?> seconds</p>
<p>Total elapsed time: <?php echo $time_end; ?> seconds</p>
<table border=1>
<thead><tr><th>Algorithm</th><th>Execution Time (s)</th><th>Hashed Output</th></tr></thead>
<tbody>
<?php echo $table_html; ?>
</tbody>
</table>
</html>
参见http://us.php.net/manual/en/function.microtime.php。基本上,像这样:
$input = "blah";
$start = microtime(TRUE);
for($i=0;$i<1000;$i++)
sha1($input);
$end = microtime(TRUE);
print "Took ".($end-$start)." sec for 1000 sha1s."