April 11th, 2007 by William Yang
On PHP’s MD5() documentation page, tszming at gmail dot com states by calling MD5() with raw_output option turned on and call bin2hex() function to convert it to hexadecimal is 3 times faster than using just MD5() function.
This sounds stupidly impossible, surely by calling one pre-built function should be faster than calling two pre-built functions with same functionality. But tszming is right, calling MD5 along is slower than calling bin2hex(md5()).
This is my test.
First test will create 1000 randomly generated string (ASCII 0 to 250). The test array of strings will be passed to the two functions to measure the time it takes to complete the operation.
Result:
|
Test 1 |
Test 2 |
Test 3 |
Test 4 |
Test 5 |
Average |
| SimpleMD5 |
7.05 ms |
6.91 ms |
6.76 ms |
6.74 ms |
6.70 ms |
6.83 ms |
| BinMD5 |
2.49 ms |
2.32 ms |
2.61 ms |
2.45 ms |
2.27 ms |
2.43 ms |
Test Methods:
SimpleMD5 - Convert string to MD5 hash string using MD5 without raw_output option
foreach ($test_array as $str) {
$tmp[] = md5($str);
}
BinMD5 - Convert string to MD5 hash string using tszming’s method, using raw_output option and use bin2hex() to convert them to hexadecimal.
foreach ($test_array as $str) {
$tmp[] = bin2hex(md5($str, true));
}
Full Source Code:
Show Code |
Download md5test.txt
<?
function microtime_float()
{
return ((float)$usec + (float)$sec);
}
function createRandomAlphanum($len = 8) {
$chars = "abcdefghijkmnopqrstuvwxyz023456789";
$i = 0;
$pass = '' ;
while ($i <= $len) {
$tmp =
substr($chars,
$num,
1);
$pass = $pass . $tmp;
$i++;
}
return $pass;
}
function createRandomString($len = 8) {
$i = 0;
$pass = '' ;
while ($i <= $len) {
$pass = $pass . $tmp;
$i++;
}
return $pass;
}
$max_element = 1000;
// Initialize array, filling random keys and values
for ($i = 0; $i <= $max_element; $i++) {
$test_array[$i] = createRandomString(1000);
}
function SimpleMD5 () {
foreach ($test_array as $str) {
}
return $tmp;
}
function BinMD5 () {
foreach ($test_array as $str) {
}
return $tmp;
}
'SimpleMD5', 'BinMD5',
);
$test_times = 5;
foreach ($tests as $test) {
// Foreach test
for ($a=1; $a<=$test_times; $a++) {
$timestart = microtime_float();
$time_took = microtime_float() - $timestart;
$test_score[$test][] = $time_took*1000;
}
}
// Makesure results match
for ($a=1; $a<=$test_times; $a++) {
foreach ($results['SimpleMD5'][$a] as $k=>$val) {
echo $val .
" = " .
$results['BinMD5'][$a][$k] .
" : " .
($val==
$results['BinMD5'][$a][$k]) .
"<br>";
}
}
?>
<table cellpadding=4 border=0 cellspacing=2>
<tr>
<th> </th>
<? for ($a=1; $a<=$test_times; $a++) { ?>
<th>Test <?=$a; ?></th>
<? } ?>
<th>Average</th>
</tr>
<? foreach ($tests as $test) { ?>
<tr>
<td><?=$test; ?></td>
<? foreach ($test_score[$test] as $score) { ?>
<? } ?>
</tr>
<? } ?>
</table>
Note: Test enviroment is PHP5 + Windows
This entry was posted
on Wednesday, April 11th, 2007 at 1:04 am and is filed under PHP Code, Optimization.
You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a response, or trackback from your own site.
Friday, April 13th 2007 at 11:12 pm
hash(’md5′, $str) is faster than the two.
Friday, May 4th 2007 at 3:52 pm
Another interesting discovery. I will put it to the test.
Tuesday, May 8th 2007 at 9:17 am
I just made the test, looks hash(’md5′, $str) is almost the same speed, if not any faster.
BinMD5 0.86 ms 0.81 ms 0.81 ms 0.80 ms 0.83 ms 0.82 ms
HashMD5 0.86 ms 0.86 ms 0.87 ms 0.86 ms 0.88 ms 0.86 ms