How can I measure the relative performance cost of different hashing methods in PHP?

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

Answers


The PHP.net page for hash has some sample code in the comments that demonstrates how you can profile the various PHP hashing functions.

Here is one person's recorded times, along with some sample code on how you can replicate the test.


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>

Need Your Help

How to create a session between client and self-hosted WCF service

wcf session cookies httpcontext self-hosting

My goal is to keep track of clients with cookies and their sessions. This is needed because I don't like WCF's builtin authentication process, where they are just give the generic login popup in the

CSS: How to make background color around text not flush to the text?

css menu background hover

I am new to CSS and website design. See the menu on the left on this page: