今天在找速度最快的 hash function
不必考慮安全因素
只要速度!!
主要用途是給css, js加上 hash name
找到一個叫 xxhash 的方法
比 md5 快了 16 倍啊!!
Name | Speed | Quality | Author |
---|---|---|---|
xxHash | 5.4 GB/s | 10 | Y.C. |
MurmurHash 3a | 2.7 GB/s | 10 | Austin Appleby |
SBox | 1.4 GB/s | 9 | Bret Mulvey |
Lookup3 | 1.2 GB/s | 9 | Bob Jenkins |
CityHash64 | 1.05 GB/s | 10 | Pike & Alakuijala |
FNV | 0.55 GB/s | 5 | Fowler, Noll, Vo |
CRC32 | 0.43 GB/s | 9 | |
MD5-32 | 0.33 GB/s | 10 | Ronald L.Rivest |
SHA1-32 | 0.28 GB/s | 10 |
那我也裝起來實驗看看摟
我用 phpbrew 裝起來超方便
$ phpbrew ext install https://github.com/Megasaxon/php-xxhash
一般安裝的話也不難
只是要記得自己加 ini 設定啟用
然後就是寫 benchmark code
<?php
// set no limit memory
ini_set("memory_limit","-1");
// get a large file content
$filePath = file_get_contents('xxx');
$testTime = 10;
$start = microtime(true);
for ($i = 0; $i < $testTime; $i++) {
md5($filePath);
}
$end = microtime(true);
echo "md5 spend time: " . number_format(($end - $start), 3) . " s";
echo PHP_EOL;
$start = microtime(true);
for ($i = 0; $i < $testTime; $i++) {
xxhash32($filePath);
}
$end = microtime(true);
echo "xxhash32 spend time: " . number_format(($end - $start), 3) . " s";
結果
$ php test_xxhash.php
md5 spend time: 17.705 s
xxhash32 spend time: 1.539 s
實測的結果也的確超過 16 倍!!!