php环境中各种hash算法速度比较

简介

本文主要介绍在php环境下,php本身所集成的各种hash(哈希)算法速度比较

环境

  • php7

测试代码

<?php

// 测试数据
$data = "hello";

// 各算法循环次数
$loop = 100000;

$results = [];

// hash算法
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']);
}

测试结果

adler32          0.0263 062c0215
joaat            0.0264 c8fd181b
fnv1a32          0.0267 4f9f2cab
fnv1a64          0.0268 a430d84680aabd0b
fnv132           0.0275 b6fa7167
fnv164           0.0280 7b495389bdbdd4c7
crc32b           0.0293 3610a686
crc32            0.0367 3d653119
md4              0.0385 866437cb7a794bce2b727acc0362ee27
md5              0.0411 5d41402abc4b2a76b9719d911017c592
tiger128,3       0.0442 2cfd7f6f336288a7f2741b9bf874388a
tiger160,3       0.0454 2cfd7f6f336288a7f2741b9bf874388a54026639
tiger192,3       0.0457 2cfd7f6f336288a7f2741b9bf874388a54026639cadb7bf2
sha1             0.0469 aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d
tiger128,4       0.0486 e8e50e239f932a1c357194e5ead0f528
tiger160,4       0.0488 e8e50e239f932a1c357194e5ead0f528dc2aebfe
tiger192,4       0.0492 e8e50e239f932a1c357194e5ead0f528dc2aebfeaed01c74
ripemd128        0.0543 789d569f08ed7055e94b4289a4195012
ripemd256        0.0573 cc1d2594aece0a064b7aed75a57283d9490fd5705ed3d66bf9adfe3a58b25de5
ripemd160        0.0675 108f07b8382412612c048d07d13f814118445acd
ripemd320        0.0714 eb0cf45114c56a8421fbcb33430fa22e0cd607560a88bbe14ce70bdf59bf55b11a3906987c487992
sha256           0.0758 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
sha224           0.0762 ea09ae9cc6768c50fcee903ed054556e5bfc8347907f12598aa24193
haval192,3       0.0913 bfaf81218bbb8ee51b600f5088c4b8601558ff56e2de1c4f
haval160,3       0.0920 0e53b29ad41cea507a343cdd8b62106864f6b3fe
sha384           0.0930 59e1748777448c69de6b800d7a33bbfb9ff1b463e44354c3553bcdb9c666fa90125a3c79f90397bdf5f6a13de828684f
haval224,3       0.0938 92d0e3354be5d525616f217660e0f860b5d472a9cb99d6766be90b15
whirlpool        0.0971 0a25f55d7308eca6b9567a7ed3bd1b46327f0f1ffdc804dd8bb5af40e88d78b88df0d002a89e2fdbd5876c523f1b67bc44e9f87047598e7548298ea1c81cfd73
sha512           0.0972 9b71d224bd62f3785d96d46ad3ea3d73319bfbc2890caadae2dff72519673ca72323c3d99ba5c11d7c7acc6e14b8c5da0c4663475c2e5c3adef46f73bcdec043
haval128,3       0.0987 85c3e4fac0ba4d85519978fdc3d1d9be
haval256,3       0.1137 26718e4fb05595cb8703a672a8ae91eea071cac5e7426173d4c25a611c4b8022
haval192,4       0.1139 ae73833a09e84691d0214f360ee5027396f12599e3618118
haval224,4       0.1143 e1ad67dc7a5901496b15dab92c2715de4b120af2baf661ecd9266317
haval256,4       0.1145 2d39577df3a6a63168826b2a10f07a65a676f5776a0772e0a877e27ec3c4c0ad
haval160,4       0.1147 b9afd44b015f8afce44e4e02d8b908ed857afbd1
haval128,4       0.1209 fe10754e0b31d69d4ece9c7a46e044e5
haval192,5       0.1322 bbb99b1e989ec3174019b20792fd92dd67175c2ff6ce5965
haval160,5       0.1330 dac5e2024bfea142e53d1422b90c9ee2c8187cc6
haval224,5       0.1343 aa6551d75e33a9c5cd4141e9a068b1fc7b6d847f85c3ab1629578ed3
haval256,5       0.1351 348298791817d5088a6de6c1b6364756d404a50bd64e645035f8cd4291c482c7
haval128,5       0.1384 d20e920d5be9d9d34855accb501d1987
gost             0.1954 a7eb5d08ddf2363f1ea0317a803fcef81d33863c8b2f9f6d7d14951d229f4567
gost-crypto      0.1989 92ea6ddbaf40020df3651f278fd7151217a24aa8d22ebd2519cfd4d89e6450ea
snefru           0.2492 7c5f22b1a92d9470efea37ec6ed00b2357a4ce3c41aa6e28e3b84057465dbb56
snefru256        0.2669 7c5f22b1a92d9470efea37ec6ed00b2357a4ce3c41aa6e28e3b84057465dbb56
md2              0.4403 a9046c73e00331af68917d3804f70655

发表评论

电子邮件地址不会被公开。 必填项已用*标注