Computing hashes in PHP
I've done a little benchmark of PHP5's hashing functions, and especially the two most widely used hashing algorithms: MD5 and SHA-1. Results are surprising: PHP's default methods (namely, md5() and sha1()), aren't optimal at all... although there are faster equivalents included in one of the modules activated by default since PHP 5.1.2! The hash module offers functions to compute hashes of strings (hash()) or files (hash_file()), and even incremental (streaming) hashes, or HMAC keyed hashes!
My benchmark was done on a quad-Xeon machine, running Debian, with many custom-built packages; so take the results for what they are: the representation of a particular situation. The general conclusions drawn from the test are widely-applicable, though. SHA-256 hashing is included to show that even more complex hashing algorithms are executed faster than simpler ones using the default PHP functions. Please note that there is no "native" function to compute SHA-256 hashes in PHP.

- Time needed to compute 5 000 000 MD5 checksums: 35.018 s.
- Time needed to compute 5 000 000 MD5 checksums (using hash()): 19.116 s.
- Time needed to compute 5 000 000 SHA-1 checksums: 40.193 s.
- Time needed to compute 5 000 000 SHA-1 checksums (using hash()): 20.057 s.
- Time needed to compute 5 000 000 SHA-256 checksums: 22.045 s.
Example code for the hash module:
<?php
print_r(hash_algos());
// 93341fd331cd6340fee05e1032297a40
var_dump(hash('md5', 'The quick brown fox blah blah blah.'));
?>
Commentaires
1. Le dimanche 10 février 2008 à 00:44, par Jedi
2. Le lundi 11 février 2008 à 22:05, par balbinus
Ajouter un commentaire
Les commentaires pour ce billet sont fermés.