Created
September 26, 2017 02:48
-
-
Save KarelWintersky/9832c335b2e48118822f6494304a781c to your computer and use it in GitHub Desktop.
PHP HASH algorithms comparsion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* User: Arris | |
* Date: 21.09.2017, time: 0:35 | |
*/ | |
const TEST_COUNT = 1000000; | |
const SOURCE = 'Тестируем построение хэша php с помощью стандартной библиотеки HASH::'; | |
function TESTER( $testing_function, $argument ) | |
{ | |
$t = microtime(true); | |
for ($test_iterator = 0; $test_iterator < TEST_COUNT; $test_iterator++) { | |
$testing_function( $argument ); | |
} | |
return round(microtime(true) - $t, 4); | |
} | |
$hasher = function($algo) { | |
hash($algo, SOURCE); | |
}; | |
$methods = hash_algos(); | |
$timings = array(); | |
foreach ($methods as $algo) { | |
$time = TESTER( $hasher, $algo ); | |
$timings[ $algo ] = $time; | |
echo str_pad($algo, 20, ' ', STR_PAD_LEFT), " have time ", str_pad($time, 8, STR_PAD_LEFT), ' seconds. ', PHP_EOL; | |
} | |
uasort($timings, function($a, $b){ | |
return $a <=> $b; | |
}); | |
$min_time = round(reset($timings) / TEST_COUNT, 7); | |
$min_hash = key($timings); | |
$max_time = round(end($timings) / TEST_COUNT, 7); | |
$max_hash = key($timings); | |
echo '-------------', PHP_EOL; | |
echo "Total tests: ", count($timings), PHP_EOL; | |
echo "Max timing : {$max_time} seconds for `{$max_hash}` algorithm.", PHP_EOL; | |
echo "Min timing : {$min_time} seconds for `{$min_hash}` algorithm.", PHP_EOL; | |
echo 'Details: ', PHP_EOL; | |
foreach ($timings as $m => $t) { | |
echo '- ', str_pad($t, 8, STR_PAD_LEFT), " seconds for `{$m}`", PHP_EOL; | |
} | |
echo PHP_EOL; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hardware: GA-N3150N-D3V, Celeron™ N3150 (1.6 GHz) quad-core processor, 4Gb RAM
Software: Gentoo 4.2.15, PHP version 7.0.2
Total tests: 46
Slowest : 0.000 026 9 seconds for
md2
algorithm.Fastest : 0.000 001 seconds for
fnv132
algorithm.Details:
fnv132
fnv1a32
adler32
fnv164
fnv1a64
joaat
crc32b
crc32
md5
md4
tiger128,3
tiger160,3
tiger192,3
tiger128,4
tiger160,4
tiger192,4
sha1
ripemd128
ripemd256
haval128,3
haval192,3
haval160,3
ripemd160
haval224,3
haval256,3
ripemd320
sha384
sha512
sha224
sha256
haval128,4
haval160,4
haval192,4
haval224,4
haval256,4
haval128,5
haval160,5
haval192,5
haval224,5
haval256,5
gost
gost-crypto
whirlpool
snefru
snefru256
md2