How to measure the relative execution cost of various hashing methods in PHP?

I would like to know how long each hashing algorithm takes a particular system, given the different lengths of the data set.

+4
source share
3 answers

I hacked this script. I recommend playing with the value of $ random_len. Different values ​​give 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> 
+2
source

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

One person is written here , as well as an example of a code by which you can replicate a test.

+2
source

See http://us.php.net/manual/en/function.microtime.php . Basically, something like this:

 $input = "blah"; $start = microtime(TRUE); for($i=0;$i<1000;$i++) sha1($input); $end = microtime(TRUE); print "Took ".($end-$start)." sec for 1000 sha1s." 
0
source

Source: https://habr.com/ru/post/1383793/


All Articles