Help us understand the problem. What is going on with this article?

PHPでのハッシュ計算速度比較

More than 3 years have passed since last update.

URLからユニーク値を計算する必要があり、md5以外の速度ってどうなのだろうと気になったので実験してみました。
PHP7で10万回ハッシュ関数を通した時の処理時間の比較。
最速でユニーク値が欲しい時は crc32 がよさげ。
速いものは暗号化関数でないことが多いので使途に注意。

環境

Windows7 64bit
PHP 7.1.1

結果

10万回計算した計算時間の合計値とハッシュ値

crc32            0.0410 3d653119
adler32          0.0430 062c0215
crc32b           0.0780 3610a686
tiger128,4       0.0980 e8e50e239f932a1c357194e5ead0f528
tiger160,4       0.0980 e8e50e239f932a1c357194e5ead0f528dc2aebfe
tiger192,3       0.1080 2cfd7f6f336288a7f2741b9bf874388a54026639cadb7bf2
ripemd256        0.1090 cc1d2594aece0a064b7aed75a57283d9490fd5705ed3d66bf9adfe3a58b25de5
tiger192,4       0.1180 e8e50e239f932a1c357194e5ead0f528dc2aebfeaed01c74
ripemd320        0.1340 eb0cf45114c56a8421fbcb33430fa22e0cd607560a88bbe14ce70bdf59bf55b11a3906987c487992
tiger128,3       0.1410 2cfd7f6f336288a7f2741b9bf874388a
fnv132           0.1460 b6fa7167
joaat            0.1500 c8fd181b
fnv1a32          0.1510 4f9f2cab
fnv164           0.1520 7b495389bdbdd4c7
tiger160,3       0.1630 2cfd7f6f336288a7f2741b9bf874388a54026639
fnv1a64          0.1680 a430d84680aabd0b
ripemd128        0.1710 789d569f08ed7055e94b4289a4195012
md5              0.1720 5d41402abc4b2a76b9719d911017c592
md4              0.2130 866437cb7a794bce2b727acc0362ee27
sha1             0.2140 aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d
ripemd160        0.2380 108f07b8382412612c048d07d13f814118445acd
sha224           0.2440 ea09ae9cc6768c50fcee903ed054556e5bfc8347907f12598aa24193
sha256           0.2570 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
haval256,3       0.3060 26718e4fb05595cb8703a672a8ae91eea071cac5e7426173d4c25a611c4b8022
gost             0.3280 a7eb5d08ddf2363f1ea0317a803fcef81d33863c8b2f9f6d7d14951d229f4567
gost-crypto      0.3340 92ea6ddbaf40020df3651f278fd7151217a24aa8d22ebd2519cfd4d89e6450ea
whirlpool        0.3800 0a25f55d7308eca6b9567a7ed3bd1b46327f0f1ffdc804dd8bb5af40e88d78b88df0d002a89e2fdbd5876c523f1b67bc44e9f87047598e7548298ea1c81cfd73
haval224,5       0.4220 aa6551d75e33a9c5cd4141e9a068b1fc7b6d847f85c3ab1629578ed3
haval128,3       0.4260 85c3e4fac0ba4d85519978fdc3d1d9be
haval192,3       0.4300 bfaf81218bbb8ee51b600f5088c4b8601558ff56e2de1c4f
haval224,3       0.4320 92d0e3354be5d525616f217660e0f860b5d472a9cb99d6766be90b15
haval160,4       0.4340 b9afd44b015f8afce44e4e02d8b908ed857afbd1
haval160,3       0.4470 0e53b29ad41cea507a343cdd8b62106864f6b3fe
haval128,4       0.4480 fe10754e0b31d69d4ece9c7a46e044e5
haval192,5       0.4680 bbb99b1e989ec3174019b20792fd92dd67175c2ff6ce5965
haval160,5       0.4930 dac5e2024bfea142e53d1422b90c9ee2c8187cc6
sha512/224       0.5350 fe8509ed1fb7dcefc27e6ac1a80eddbec4cb3d2c6fe565244374061c
sha384           0.5510 59e1748777448c69de6b800d7a33bbfb9ff1b463e44354c3553bcdb9c666fa90125a3c79f90397bdf5f6a13de828684f
sha512/256       0.5670 e30d87cfa2a75db545eac4d61baf970366a8357c7f72fa95b52d0accb698f13a
sha512           0.5710 9b71d224bd62f3785d96d46ad3ea3d73319bfbc2890caadae2dff72519673ca72323c3d99ba5c11d7c7acc6e14b8c5da0c4663475c2e5c3adef46f73bcdec043
haval128,5       0.5860 d20e920d5be9d9d34855accb501d1987
haval256,5       0.5870 348298791817d5088a6de6c1b6364756d404a50bd64e645035f8cd4291c482c7
haval192,4       0.5920 ae73833a09e84691d0214f360ee5027396f12599e3618118
snefru256        0.6000 7c5f22b1a92d9470efea37ec6ed00b2357a4ce3c41aa6e28e3b84057465dbb56
haval224,4       0.6080 e1ad67dc7a5901496b15dab92c2715de4b120af2baf661ecd9266317
haval256,4       0.6140 2d39577df3a6a63168826b2a10f07a65a676f5776a0772e0a877e27ec3c4c0ad
snefru           0.6320 7c5f22b1a92d9470efea37ec6ed00b2357a4ce3c41aa6e28e3b84057465dbb56
md2              1.9160 a9046c73e00331af68917d3804f70655
sha3-384         3.5290 720aea11019ef06440fbf05d87aa24680a2153df3907b23631e7177ce620fa1330ff07c0fddee54699a4c3ee0ee9d887
sha3-256         5.0450 3338be694f50c5f338814986cdf0686453a888b84f424d792af4b9202398f392
sha3-512         5.1080 75d527c368f2efe848ecf6b073a36767800805e9eef2b1857d5f984f036eb6df891d75f72d9b154518c1cd58835286d1da9a38deba3de98b5a53e5ed78a84976
sha3-224         5.5110 b87f88c72702fff1748e58b87e9141a42c0dbedc29a78cb0d4a5cd81

実行コード

$data = "hello";
$loop = 100000;
$results = [];

// ハッシュ化
foreach (hash_algos() as $algo) {
  $total = 0;
  for ($i=0; $i < $loop; $i++) {
    $begin = microtime(true);
    $hashed = hash($algo, $data, false);
    $total += microtime(true) - $begin;
  }
  $results[] = [
    'name'   => $algo,
    'time'   => $total,
    'hashed' => $hashed,
  ];
}

// 速い順にソート
usort($results, function($a, $b) {
  return $a['time'] <=> $b['time'];
});

// 表示
foreach ($results as $result) {
  printf("%-16s %01.4f %s\n", $result['name'], $result['time'], $result['hashed']);
}
ichi_404
`${ new Date().getFullYear() - 2015 }` 年目 PHP、JavaScript、猫と戯れる。
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away