PHP从5.1.2版本以上开始支持hash_algos函数,看这个名字就知道了,algos在英文中也表示算法的意思,hash_algos就是哈希算法,收集了一些常用的哈希算法,从5.1.2开始不同版本的PHP,支持的算法也都略有不同。
在这里,我写一段函数,可以看到我这边版本PHP所支持的所有哈希算法,并计算出他们的性能对比。
<?php
set_time_limit(300);
define(‘CNT‘, 50000);
$algos = hash_algos();
foreach($algos as $algo) {
$st = microtime();
for($i = 0; $i < CNT; $i++) {
hash($algo, microtime().$i);
}
$et = microtime();
list($ss, $si) = explode(‘ ‘, $st);
list($es, $ei) = explode(‘ ‘, $et);
$time[$algo] = $ei + $es - $si - $ss;
$time[$algo] .= " [sample:" . hash($algo, "HELLO WORLD") . "]";
}
asort($time, SORT_NUMERIC);
echo "<table>";
foreach($time as $key=>$value){
echo "<tr><td>$key</td><td>$value</td></tr>";
}
echo "</table>";
得到的结果如下:
fnv164 | 0.37478698735046 [sample:eaab5a330ee5b36f] |
md4 | 0.3985438946991 [sample:c85e5fdf3a18840f9041ac70f241deb0] |
crc32b | 0.41961007745361 [sample:87e5865b] |
fnv132 | 0.42863700695801 [sample:4240512f] |
tiger160,3 | 0.43971493786621 [sample:5ffd501e367bed4d8e103761f66fad075ff4af41] |
adler32 | 0.44000594578552 [sample:128b031d] |
joaat | 0.46947299908447 [sample:0b1cc760] |
ripemd128 | 0.49671197273254 [sample:34f326051d6c45d698add89721159ce4] |
sha224 | 0.51215290837097 [sample:906bbee30415aab29f8309b829b521bb4275de9a302b37480ab129e9] |
md5 | 0.52081801637268 [sample:361fadf1c712e812d198c4cab5712a79] |
sha1 | 0.53381191786194 [sample:4b68507f1746b0e5f3efe99b8ef42afef79da017] |
sha256 | 0.54046505882263 [sample:787ec76dcafd20c1908eb0936a12f91edd105ab5cd7ecc2b1ae2032648345dff] |
tiger128,3 | 0.54758100090027 [sample:5ffd501e367bed4d8e103761f66fad07] |
ripemd160 | 0.5674130983429 [sample:3219b86078ed4b3da3d7cf681144ab933c0938b2] |
crc32 | 0.58168091377258 [sample:299ffb96] |
haval128,3 | 0.59623603713989 [sample:10b8533d416be012f54f5bf090635f2a] |
ripemd256 | 0.598747929245 [sample:281921c018530c2c00681cb9f7970368888a900e79160ed72bd578704fe1eef5] |
haval256,3 | 0.60667002838135 [sample:1534f6bfda22092180e3b737616f1e9b8f91508ead0199bb831d3a2ba57b621d] |
ripemd320 | 0.63686393048096 [sample:89e3861212449bbd2577a890a02be389b46afef606c4095302b182f7b88ab1a5b0e838095dc3a80e] |
haval192,3 | 0.65839607575989 [sample:cfaf464019fc31f8e3346816ac195dc44314ee5061d47f81] |
haval160,3 | 0.75110392315674 [sample:c937a75c444ce72fc7e0f216ee4fd66067f7c85d] |
whirlpool | 0.75960894284058 [sample:153800a4da7dab37cf8d985f38edb51e34aa1c38b10db2c66a6727b249198df07ca0ecdd008bf2b3d3e5c6fddcc5e81b67ef5f090042941a9dcf8a6eabd6a36c] |
haval192,4 | 0.7767040065155 [sample:f231f85b193f40310bd063723d6645d9315729fc83daa729] |
haval224,3 | 0.78067902493286 [sample:a3ea3ea3b3f39f7417c75132e0a49ef8d06e4af1bfbe9fb6a448f72b] |
tiger192,3 | 0.78198196318054 [sample:5ffd501e367bed4d8e103761f66fad075ff4af41b5b9b31e] |
sha512 | 0.79405710838318 [sample:13d6c73ac8cceeff9ff6b0ba2ce19c5fc47ac21f9fd403c151fe88e0fd39f4223c29bc9bded59e1e3f272fd969fd6e2e6e35be35072e742c4b36fec48feb87df] |
sha384 | 0.82325292324829 [sample:d3e23b4f16ea11c71aba0d23ba04a5625482a33bdcc69e63e2b86122470d22cf906bdbbcb204e73ec1338c8508bb2e2a] |
haval256,4 | 0.83586111834717 [sample:ca671b39d8bf0bbb3d24a6883a4b907cd9413591d69e8b5c775dd6a2168570f0] |
haval128,4 | 0.84537810420227 [sample:fa039d416e0ebb1a8e4d2e09e1187956] |
tiger160,4 | 0.85183900819397 [sample:10f15bbde5f13452c365c54d1623ba99b0c052be] |
haval224,5 | 0.8582429559021 [sample:0ad3fd38c1fdaa22c425af43ba805893ae8b1daf7b2e8b2c1910821a] |
haval160,4 | 0.85876092556763 [sample:85937b88a871dc814e70b9fb185ea9c6c0f698e5] |
haval256,5 | 0.91796088124084 [sample:90c751cd8f632217af98f8d72e9da4123ed7221c6091fa877e1a158264cba3a6] |
haval192,5 | 0.94244197169495 [sample:f64e4497f83c6a91ec5dcc0e2c074593b5ee37898304153d] |
tiger192,4 | 0.95719099984741 [sample:10f15bbde5f13452c365c54d1623ba99b0c052be228b4e5a] |
haval160,5 | 1.020770897995 [sample:c091f42b8f48de9c6a9cbaecc2f2962b74d882f8] |
haval224,4 | 1.0293329506226 [sample:bb68e9358e71067cd20db808bfeb69575772ee4e8c11b51431672fd4] |
haval128,5 | 1.0518960091553 [sample:69b36b8ffb85c66a395137cc89cd70bd] |
tiger128,4 | 1.0905900340881 [sample:10f15bbde5f13452c365c54d1623ba99] |
gost | 1.1367739732208 [sample:b0278f172b9f94c86d8344c28e58b8727c0396f6dfb11648070ec188d53bef44] |
md2 | 1.2356340056915 [sample:8a4b081d657c2099cbfaee796217759f] |
snefru256 | 1.381886079422 [sample:2ffb1850bb73024a262f842c5e7dbbe37256b6a1562b8b6c95d3a5c0d3b29f5b] |
snefru | 1.6106969665833 [sample:2ffb1850bb73024a262f842c5e7dbbe37256b6a1562b8b6c95d3a5c0d3b29f5b] |
可以看出fnv164最快,Sha512和md5在性能上对比,MD5会稍微快一些,但是不明显,而Sha512却比md5安全多了,不过由于占的位数太长,存储空间占用不少,如果使用md5(sha512())的话,是比较折衷且优秀的算法。
文章转载自:http://www.ttmd5.com/article.php?id=14