我如何测量PHP中不同哈希方法的相对性能成本? [英] How can I measure the relative performance cost of different hashing methods in PHP?

查看:113
本文介绍了我如何测量PHP中不同哈希方法的相对性能成本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道每个哈希算法在给定不同数据集长度的情况下对特定系统的处理时长。

掀起了这个剧本。我建议玩$ random_len值。不同的值会产生一些有趣的结果。

 <?php 
$ random_len = 100; / *字节* /
$ 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>'。 $阿尔戈。 '< / td>< td>'。 $结束。 '< / td>< td>'。 $哈希。 < / TD>< / TR>;
}
$ time_end = microtime(true) - $ time_begin;
?>
< html>
< style> body {font-family:monospace} td {white-space:nowrap}< / style>

< h1> PHP哈希算法执行时间< / h1>
< p>随机数据长度:<?php echo $ random_len; ?>字节< / p为H.
< p>随机数据流逝时间:<?php echo $ time_rand; ?>秒-1; / p为H.
< p>总运行时间:<?php echo $ time_end; ?>秒-1; / p为H.

< table border = 1> $ s $ b< thead>< tr>< th>算法< th>执行时间>< th> < / THEAD>
< tbody>
<?php echo $ table_html; ?>
< / tbody>
< / table>
< / html>


I'd like to know how long each hashing algorithm takes on a particular system given different dataset lengths.

解决方案

I whipped up this script. I recommend playing with the $random_len value. Differing values produces some interesting results.

<?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>

这篇关于我如何测量PHP中不同哈希方法的相对性能成本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆