Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
PHP HASH algorithms comparsion
<?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;
@KarelWintersky

This comment has been minimized.

Copy link
Owner Author

commented Sep 26, 2017

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:

  • 0.978500 seconds for fnv132
  • 0.985000 seconds for fnv1a32
  • 1.026300 seconds for adler32
  • 1.132500 seconds for fnv164
  • 1.133400 seconds for fnv1a64
  • 1.151100 seconds for joaat
  • 1.219300 seconds for crc32b
  • 1.273600 seconds for crc32
  • 1.519600 seconds for md5
  • 1.673900 seconds for md4
  • 2.048900 seconds for tiger128,3
  • 2.066500 seconds for tiger160,3
  • 2.107500 seconds for tiger192,3
  • 2.377700 seconds for tiger128,4
  • 2.401100 seconds for tiger160,4
  • 2.432900 seconds for tiger192,4
  • 2.581300 seconds for sha1
  • 3.190700 seconds for ripemd128
  • 3.287700 seconds for ripemd256
  • 4.339700 seconds for haval128,3
  • 4.386900 seconds for haval192,3
  • 4.416600 seconds for haval160,3
  • 4.418200 seconds for ripemd160
  • 4.419600 seconds for haval224,3
  • 4.451000 seconds for haval256,3
  • 4.538500 seconds for ripemd320
  • 4.877700 seconds for sha384
  • 4.966900 seconds for sha512
  • 5.392500 seconds for sha224
  • 5.408600 seconds for sha256
  • 5.618100 seconds for haval128,4
  • 5.638600 seconds for haval160,4
  • 5.662400 seconds for haval192,4
  • 5.693000 seconds for haval224,4
  • 5.720700 seconds for haval256,4
  • 6.580900 seconds for haval128,5
  • 6.601800 seconds for haval160,5
  • 6.624600 seconds for haval192,5
  • 6.650100 seconds for haval224,5
  • 6.689300 seconds for haval256,5
  • 7.471700 seconds for gost
  • 7.495900 seconds for gost-crypto
  • 7.875500 seconds for whirlpool
  • 10.93680 seconds for snefru
  • 10.99100 seconds for snefru256
  • 26.91360 seconds for md2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.