Created
July 10, 2014 04:58
-
-
Save bwg/c4c13c828cf05a952bae to your computer and use it in GitHub Desktop.
md5 vs. sha1 vs. crc32
This file contains hidden or 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 Version 5.4.30 | |
-------------------------------------------------- | |
Testing 0 char string over 100,000 iterations: | |
md5 113.2709980011 ms | |
sha1 117.16985702515 ms | |
crc32 78.847885131836 ms | |
-------------------------------------------------- | |
Testing 1 char string over 100,000 iterations: | |
md5 105.99207878113 ms | |
sha1 114.00294303894 ms | |
crc32 76.585054397583 ms | |
-------------------------------------------------- | |
Testing 5 char string over 100,000 iterations: | |
md5 106.14204406738 ms | |
sha1 113.75713348389 ms | |
crc32 78.852891921997 ms | |
-------------------------------------------------- | |
Testing 50 char string over 100,000 iterations: | |
md5 104.63809967041 ms | |
sha1 115.33093452454 ms | |
crc32 89.761018753052 ms | |
-------------------------------------------------- | |
Testing 100 char string over 100,000 iterations: | |
md5 117.97094345093 ms | |
sha1 128.39221954346 ms | |
crc32 98.706960678101 ms | |
-------------------------------------------------- | |
Testing 1000 char string over 100,000 iterations: | |
md5 294.61193084717 ms | |
sha1 314.38899040222 ms | |
crc32 287.9581451416 ms | |
-------------------------------------------------- | |
average (ms) total (ms) | |
-------------------------------------------------- | |
md5 140.43768246969 842.62609481812 | |
sha1 150.50701300303 903.04207801819 | |
crc32 118.45199267069 710.71195602417 |
This file contains hidden or 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 | |
$iterations = 100000; | |
function randomString($len) { | |
$chars = "0123456789abcdefghijklmnopqrstuvwxyz"; | |
return substr(str_shuffle(str_repeat($chars, ceil($len/strlen($chars)))), 0, $len); | |
} | |
$values = [ | |
'0' => '', | |
'1' => randomString(1), | |
'5' => randomString(5), | |
'50' => randomString(50), | |
'100' => randomString(100), | |
'1000' => randomString(1000), | |
]; | |
$totals = [ | |
'md5' => 0, | |
'sha1' => 0, | |
'crc32' => 0, | |
]; | |
echo "PHP Version ".phpversion()."\n\n"; | |
foreach ($values as $name => $value) { | |
echo str_repeat('-', 50)."\n"; | |
echo "Testing {$name} char string over ".number_format($iterations)." iterations:\n\n"; | |
$start = microtime(true); | |
for ($i = 0; $i < $iterations; $i++) { | |
md5($value); | |
} | |
$end = microtime(true); | |
$duration = ($end - $start) * 1000; | |
$totals['md5'] += $duration; | |
echo str_pad('md5', 30)."{$duration} ms\n"; | |
//----------------------------------------------------------------------------- | |
$start = microtime(true); | |
for ($i = 0; $i < $iterations; $i++) { | |
sha1($value); | |
} | |
$end = microtime(true); | |
$duration = ($end - $start) * 1000; | |
$totals['sha1'] += $duration; | |
echo str_pad('sha1', 30)."{$duration} ms\n"; | |
//----------------------------------------------------------------------------- | |
$start = microtime(true); | |
for ($i = 0; $i < $iterations; $i++) { | |
crc32($value); | |
} | |
$end = microtime(true); | |
$duration = ($end - $start) * 1000; | |
$totals['crc32'] += $duration; | |
echo str_pad('crc32', 30)."{$duration} ms\n"; | |
//----------------------------------------------------------------------------- | |
} | |
echo "\n".str_repeat('-', 50)."\n"; | |
echo str_pad('', 15); | |
echo str_pad('average (ms)', 20); | |
echo str_pad('total (ms)', 20)."\n"; | |
echo str_repeat('-', 50)."\n"; | |
foreach ($totals as $name => $total) { | |
echo str_pad($name, 15); | |
echo str_pad($total/count($values), 20); | |
echo str_pad($total, 20)."\n"; | |
} | |
echo "\n"; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment