ralph l mayo
12-10-2005, 01:02 AM
I just wrote this recently and have been using it quite a lot. Whenever I have creative block I can go back and try to refactor things with it. It's not as robust as the Apache bench tool or anything, but I still find it handy:
function bench($func1, $func2, $laps = 1000, $trials = 11)
{
$index[0] = preg_replace('/(.+)\(.*\).*/', '$1()', $func1);
$index[1] = preg_replace('/(.+)\(.*\).*/', '$1()', $func2);
if ($index[0] == $index[1])
{
$index[1] .= '-2';
}
for ($trial = 0; $trial < $trials; ++$trial)
{
foreach (array($func1, $func2) as $key=>$func)
{
$results[$trial][$index[$key]]['starttime'] = microtime(true);
for ($lap = 0; $lap < $laps; ++$lap)
{
eval($func);
}
$results[$trial][$index[$key]]['endtime'] = microtime(true);
}
}
// calculations are separated from the main loop to prevent interference
// also, ugly code warning... most people probably don't share my sick love of multidimensional arrays
for ($trial = 0; $trial < $trials; ++$trial)
{
$results[$trial][$index[0]]['elapsedtime'] = $results[$trial][$index[0]]['endtime'] - $results[$trial][$index[0]]['starttime'];
$results[$trial][$index[1]]['elapsedtime'] = $results[$trial][$index[1]]['endtime'] - $results[$trial][$index[1]]['starttime'];
$tempindex = ($results[$trial][$index[0]]['elapsedtime'] < $results[$trial][$index[1]]['elapsedtime']) ? array('winner'=>$index[0], 'loser'=>$index[1]) : array('winner'=>$index[1], 'loser'=>$index[0]);
$results[$trial]['winner'] = $tempindex['winner'];
$results[$trial]['ratio'] = $results[$trial][$tempindex['loser']]['elapsedtime'] / $results[$trial][$tempindex['winner']]['elapsedtime'];
}
return($results);
}
A usage example:
// don't use this stupid function, it's just here to test
function bbcodestr($text)
{
return(str_ireplace(array('','','', '', '', ''), array('<b>', '<u>','<i>', '</b>', '</u>', '</i>'), $text));
}
// this one either
function bbcodepreg($text)
{
return(preg_replace(array('/\[()\]/i', '/\[\/([biu])\]/i'), array('<$1>', '</$1>'), $text));
}
$teststring = '[b]what is up?';
$speedtest = bench('bbcodestr(\'' . $teststring . '\');', 'bbcodepreg(\'' . $teststring . '\');', 10000); // run 10,000 times per trial, for the default 11 trials. Note the quotation marks and terminating semicolons are preserved in the function calls
//print_r($speedtest); // more detailed info
foreach ($speedtest as $key=>$trial)
{
echo 'Trial ' . ($key + 1) . ' winner: ' . $trial['winner'] . ' by a factor of ' . $trial['ratio'] . '<br />';
}
/* Sample results, YMMV:
Trial 1 winner: bbcodepreg() by a factor of 1.0701883025352
Trial 2 winner: bbcodestr() by a factor of 1.1007553220986
Trial 3 winner: bbcodestr() by a factor of 1.1745991875412
Trial 4 winner: bbcodestr() by a factor of 1.1061013972411
Trial 5 winner: bbcodestr() by a factor of 1.1113493309722
Trial 6 winner: bbcodestr() by a factor of 1.0427249521624
Trial 7 winner: bbcodestr() by a factor of 1.0399104266645
Trial 8 winner: bbcodestr() by a factor of 1.0071057292584
Trial 9 winner: bbcodestr() by a factor of 1.1914867817959
Trial 10 winner: bbcodestr() by a factor of 1.0953776958654
Trial 11 winner: bbcodestr() by a factor of 1.0666147280889
The first result tends to be fishy and is usually discarded, hence the default of 11 trials.
*/
Try not to run this on shared hosting :D
function bench($func1, $func2, $laps = 1000, $trials = 11)
{
$index[0] = preg_replace('/(.+)\(.*\).*/', '$1()', $func1);
$index[1] = preg_replace('/(.+)\(.*\).*/', '$1()', $func2);
if ($index[0] == $index[1])
{
$index[1] .= '-2';
}
for ($trial = 0; $trial < $trials; ++$trial)
{
foreach (array($func1, $func2) as $key=>$func)
{
$results[$trial][$index[$key]]['starttime'] = microtime(true);
for ($lap = 0; $lap < $laps; ++$lap)
{
eval($func);
}
$results[$trial][$index[$key]]['endtime'] = microtime(true);
}
}
// calculations are separated from the main loop to prevent interference
// also, ugly code warning... most people probably don't share my sick love of multidimensional arrays
for ($trial = 0; $trial < $trials; ++$trial)
{
$results[$trial][$index[0]]['elapsedtime'] = $results[$trial][$index[0]]['endtime'] - $results[$trial][$index[0]]['starttime'];
$results[$trial][$index[1]]['elapsedtime'] = $results[$trial][$index[1]]['endtime'] - $results[$trial][$index[1]]['starttime'];
$tempindex = ($results[$trial][$index[0]]['elapsedtime'] < $results[$trial][$index[1]]['elapsedtime']) ? array('winner'=>$index[0], 'loser'=>$index[1]) : array('winner'=>$index[1], 'loser'=>$index[0]);
$results[$trial]['winner'] = $tempindex['winner'];
$results[$trial]['ratio'] = $results[$trial][$tempindex['loser']]['elapsedtime'] / $results[$trial][$tempindex['winner']]['elapsedtime'];
}
return($results);
}
A usage example:
// don't use this stupid function, it's just here to test
function bbcodestr($text)
{
return(str_ireplace(array('','','', '', '', ''), array('<b>', '<u>','<i>', '</b>', '</u>', '</i>'), $text));
}
// this one either
function bbcodepreg($text)
{
return(preg_replace(array('/\[()\]/i', '/\[\/([biu])\]/i'), array('<$1>', '</$1>'), $text));
}
$teststring = '[b]what is up?';
$speedtest = bench('bbcodestr(\'' . $teststring . '\');', 'bbcodepreg(\'' . $teststring . '\');', 10000); // run 10,000 times per trial, for the default 11 trials. Note the quotation marks and terminating semicolons are preserved in the function calls
//print_r($speedtest); // more detailed info
foreach ($speedtest as $key=>$trial)
{
echo 'Trial ' . ($key + 1) . ' winner: ' . $trial['winner'] . ' by a factor of ' . $trial['ratio'] . '<br />';
}
/* Sample results, YMMV:
Trial 1 winner: bbcodepreg() by a factor of 1.0701883025352
Trial 2 winner: bbcodestr() by a factor of 1.1007553220986
Trial 3 winner: bbcodestr() by a factor of 1.1745991875412
Trial 4 winner: bbcodestr() by a factor of 1.1061013972411
Trial 5 winner: bbcodestr() by a factor of 1.1113493309722
Trial 6 winner: bbcodestr() by a factor of 1.0427249521624
Trial 7 winner: bbcodestr() by a factor of 1.0399104266645
Trial 8 winner: bbcodestr() by a factor of 1.0071057292584
Trial 9 winner: bbcodestr() by a factor of 1.1914867817959
Trial 10 winner: bbcodestr() by a factor of 1.0953776958654
Trial 11 winner: bbcodestr() by a factor of 1.0666147280889
The first result tends to be fishy and is usually discarded, hence the default of 11 trials.
*/
Try not to run this on shared hosting :D