Borrowed from one of BG's posts a while back:
<?php
/**************
* Test setup *
**************/
set_time_limit(0);echo '<pre>';
$funcs = array('multiRegEx', 'singleRegEx');
$params = array(
array('Pass\$word1'),
array('!Passwor2d'),
array('Passwo3\$rd'),
array('4Password%'),
array('Password'),
array('Password1'),
array('password2'),
array('PASSWORD1'),
array('P%wor1'),
array('Short1-'),
array('allLetters'),
array('nocaps123-.'),
array('Valid-Password-1'),
);
$num_times = 25000;
$unit_conv = array(1000, 'msec');
$precision = 6;
$sound = true;
/*****************************************
* Definitions of functions to be tested *
*****************************************/
function singleRegEx($password) {
$regexp = '#^.*(?=.{8,})(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[^0-9a-zA-Z]).*$#';
if( preg_match($regexp, $password) ) {
return TRUE;
} else {
return FALSE;
}
}
function multiRegEx($password) {
if( preg_match('/[A-Z]/',$password) == 1 &&
preg_match('/[a-z]/', $password) == 1 &&
preg_match('/[0-9]/', $password) == 1 &&
preg_match('/\W/', $password) == 1 &&
strlen("$password") > 7 &&
strlen("$password") < 17 ) {
return TRUE;
} else {
return FALSE;
}
}
/***********************************
* !!! END OF EDITABLE REGIONS !!! *
***********************************/
/*******************************
* Init stat-keeping variables *
*******************************/
$times = array_fill(0, count($funcs), array());
$medians = array_fill(0, count($params), array());
/*************
* Run tests *
*************/
for($func_idx = 0; $func_idx < count($funcs); $func_idx++)
{
$times[$func_idx] = array_fill(0, count($params), array());
for($param_idx = 0; $param_idx < count($params); $param_idx++)
{
for($i = 0; $i < $num_times; $i++)
{
$start = microtime(true);
call_user_func_array($funcs[$func_idx], $params[$param_idx]);
$end = microtime(true);
$times[$func_idx][$param_idx][] = ($end - $start) * $unit_conv[0];
}
// Sort stored times
sort($times[$func_idx][$param_idx]) or die('error sorting times');
// Store median value
$medians[$param_idx][$func_idx] =
$times[$func_idx][$param_idx][floor($num_times / 2)];
}
// Announce completion of testing for function
if($sound)
echo "\x07";
}
/***********************************
* Generate output of test results *
* (grouped by function) *
***********************************/
echo "Number of executions: $num_times\n\n";
for($func_idx = 0; $func_idx < count($funcs); $func_idx++)
{
echo "Function #$func_idx :\n";
for($param_idx = 0; $param_idx < count($params); $param_idx++)
{
echo "\t$funcs[$func_idx](" . implode(',', $params[$param_idx]) . ")\n";
// Variable rename for brevity
$p = $precision;
printf(
"\t\tMin: %.{$p}f %4\$s\n\t\tMax: %.{$p}f %4\$s\n"
. "\t\tAvg: %.{$p}f %4\$s\n",
$times[$func_idx][$param_idx][0],
$times[$func_idx][$param_idx][$num_times - 1],
(array_sum($times[$func_idx][$param_idx]) / $num_times),
$unit_conv[1]
);
}
}
echo "\n--------------------------------------------------\n\n";
/***********************************
* Generate output of test results *
* (grouped by parameters) *
* (based on func medians) *
***********************************/
for($param_idx = 0; $param_idx < count($params); $param_idx++)
{
echo "Parameter Group #$param_idx (" . implode(',', $params[$param_idx])
. ") :\n";
echo "\tMedians :\n";
// Sort median values
asort($medians[$param_idx]) or die('error sorting medians');;
foreach($medians[$param_idx] as $func_idx => $median)
{
// Variable rename for brevity
$p = $precision;
printf(
"\t\t%s: %.{$p}f %s\n",
$funcs[$func_idx],
$median,
$unit_conv[1]
);
}
}
Edit: Btw Getting almost the same results at 25000 and 50000 iterations with normal variance of each refresh.