function microtime_float()
{
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
function createRandomAlphanum($len = 8) {
$chars = "abcdefghijkmnopqrstuvwxyz023456789";
srand((double)microtime()*1000000);
$i = 0;
$pass = '' ;
while ($i <= $len) {
$num = rand() % 33;
$tmp = substr($chars, $num, 1);
$pass = $pass . $tmp;
$i++;
}
return $pass;
}
function createRandomString($len = 8) {
srand((double)microtime()*1000000);
$i = 0;
$pass = '' ;
while ($i <= $len) {
$num = rand() % 250;
$tmp = chr($num);
$pass = $pass . $tmp;
$i++;
}
return $pass;
}
$max_element = 1000;
$test_array = array();
// Initialize array, filling random keys and values
for ($i = 0; $i <= $max_element; $i++) {
$test_array[$i] = createRandomString(1000);
}
function SimpleMD5 () {
global $test_array;
foreach ($test_array as $str) {
$tmp[] = md5($str);
}
return $tmp;
}
function BinMD5 () {
global $test_array;
foreach ($test_array as $str) {
$tmp[] = bin2hex(md5($str, true));
}
return $tmp;
}
$tests = array(
'SimpleMD5', 'BinMD5',
);
$test_times = 5;
$test_score = array();
foreach ($tests as $test) {
// Foreach test
for ($a=1; $a<=$test_times; $a++) {
reset($test_array);
$timestart = microtime_float();
$results[$test][$a] = call_user_func($test);
$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]) . "
";
}
}
?>
| for ($a=1; $a<=$test_times; $a++) { ?> | Test =$a; ?> | } ?>Average |
|---|---|---|
| =$test; ?> | foreach ($test_score[$test] as $score) { ?>=number_format($score, 2); ?> ms | } ?>=number_format(array_sum($test_score[$test])/sizeof($test_score[$test]), 2); ?> ms |