| px | top | add code | search | signup | login | help |
<?php
// +----------------------------------------------------------------------+
// | codeSnippetBench.php |
// |======================================================================|
// | How it works:
// | Below you will see some working code as sample. To add new tests
// | add a $profileTest[] hash entry below.
// | \'title\' / \'text\' Are used for the output and are self explaining.
// | \'function\' Is the function *basename* like \"myTest\". This program
// | will then look for function \"myTest1\", \"myTest2\" a.s.o.
// | \'param\' Is the first parameter passed to that function
// |
// | Now write the function (e.g. \"myTest1\", \"myTest2\"). They will be
// | called and benchmarked.
// +----------------------------------------------------------------------+
// | Main Authors: |
// | Sam Blum <bs_php@infeer.com> |
// +----------------------------------------------------------------------+
// | Requires PHP version 4.0.3 and up |
// +----------------------------------------------------------------------+
/***
* Sample
* $profileTest[] = array (
* \'title\' => \'For-loop test\',
* \'text\' => \'Is it worth the effort to calculate the length of the loop in advance?\',
* \'function\' => \'forLoop\', // Funcion basename
* \'param\' => \'\' // This wil be passed to your function.
* );
*/
# For foreach vs while(list()=each())
$profileTest[] = array (
title => foreach() vs. while(list()=each()),
text => What is the best way to loop a hash array? <br>Given is a Hash array
. with 1000 elements, 24byte key and 100k data per entry<br>
. Ive chosen the large data amount to try out what happens if I ref. the
. data with the &-ref-operator (to avoid copying). But to my surprise
. the loops are never faster! In tests 5 and 6 are even 30x slower !! Way ???. <br>
. <strong>Let me know at bs_php@users.sourceforge.net</strong>,
function => forEach,
param => // This wil be passed to your function.
);
# For loop tests
$profileTest[] = array (
title => For-loop test,
text => Is it worth the effort to calculate the length of the loop in advance?
.<br>E.g. "for ($i=0; $i<$size; $i++)\" instead of \"for ($i=0; $i<sizeOf($x); $i++)\"\',
\'function\' => \'forLoop\',
\'param\' => \'\' // This wil be passed to your function.
);
# isSet test
$profileTest[] = array (
\'title\' => \'isSet test\',
\'text\' => \'What is the performance of isSet. Call 1\\\'000x\',
\'function\' => \'isSet\',
\'param\' => \'1000\' // This wil be passed to your function.
);
# is_array test
$profileTest[] = array (
\'title\' => \'is_array test\',
\'text\' => \'What is the performance of is_array. Call 1\\\'000x\',
\'function\' => \'isArray\',
\'param\' => \'1000\' // This wil be passed to your function.
);
# case or if elseif
$profileTest[] = array (
\'title\' => \'switch/case vs. if/elseif\',
\'text\' => \'Is a there a difference between switch and if elseif. Call 10\\\'000x\',
\'function\' => \'ifOrCase\',
\'param\' => \'10000\' // This wil be passed to your function.
);
// ------------------------------------------------------------------
// ------------------------------------------------------------------
// Add your functions below here
#----------------------------------------------------------------------
# For foreach vs while(list()=each())
//Prepare a dummy test hash
$feHash = array();
$bigData .= str_repeat(\'a\', 100000);
for ($i=0; $i<1000; $i++) {
$key=\'\';
for ($j=0; $j<24; $j++) {
$key .= chr(mt_rand(32,88));
}
$bigData .= chr(mt_rand(33,88));
$feHash[$key] = $bigData;
}
function forEach1($param, &$profTest) {
$profTest[\'TestCaption\'][] = \'foreach($feHash as $val)\';
global $feHash;
foreach($feHash as $val);
}
function forEach2($param, &$profTest) {
$profTest[\'TestCaption\'][] = \'while(list(,$val)... \';
global $feHash;
reset($feHash);
while(list(,$val) = each($feHash));
}
// ---
function forEach3($param, &$profTest) {
$profTest[\'TestCaption\'][] = \'foreach($feHash as $key=>$val)\';
global $feHash;
foreach($feHash as $key=>$val);
}
function forEach4($param, &$profTest) {
$profTest[\'TestCaption\'][] = \'while(list($key,$val)... \';
global $feHash;
reset($feHash);
while(list($key,$val) = each($feHash));
}
// ---
function forEach5($param, &$profTest) {
$profTest[\'TestCaption\'][] = \'foreach($feHash as $key=>$val) then $tmp[]=&$hash[$key] \';
global $feHash;
$i=0;
foreach($feHash as $key=>$val) $tmp[] = &$feHash[$key];
}
function forEach6($param, &$profTest) {
$profTest[\'TestCaption\'][] = \'while(list($key)... then $tmp[]=&$hash[$key] \';
global $feHash;
reset($feHash);
while(list($key) = each($feHash)) $tmp[]=&$feHash[$key];
}
// ---
function forEach7($param, &$profTest) {
$profTest[\'TestCaption\'][] = \'foreach($feHash as $key[]=>$val[])\';
global $feHash;
foreach($feHash as $key[]=>$val[]);
}
function forEach8($param, &$profTest) {
$profTest[\'TestCaption\'][] = \'array_keys() / array_values()\';
global $feHash;
$key = array_keys($feHash);
$val = array_values($feHash);
}
function forEach9($param, &$profTest) {
$profTest[\'TestCaption\'][] = \'STRANGE: This is the fasetest code when using the the &-ref-operator (to avoid copying)<br>$key = array_keys($feHash);<br>$size = sizeOf($key);<br>for ($i=0; $i<$size; $i++) $tmp[] = &$feHash[$key[$i]];\';
global $feHash;
$key = array_keys($feHash);
$size = sizeOf($key);
for ($i=0; $i<$size; $i++) $tmp[] = &$feHash[$key[$i]];
}
#----------------------------------------------------------------------
# For loop tests
// some inits:
$forLoop = str_repeat(\'a\', 10000);
function forLoop1($param, &$profTest) {
$profTest[\'TestCaption\'][] = \'With pre calc\';
global $forLoop;
$leng = strLen($forLoop);
for ($i=0; $i<$leng; $i++) ;
}
function forLoop2($param, &$profTest) {
$profTest[\'TestCaption\'][] = \'Without pre calc\';
global $forLoop;
for ($i=0; $i<strLen($forLoop); $i++) ;
}
#----------------------------------------------------------------------
# Is Set tests
function isSet1($param, &$profTest) {
$profTest[\'TestCaption\'][] = \'Simple var was set\';
$var = 1;
for ($i=0; $i<$param; $i++) isSet($var);
}
function isSet2($param, &$profTest) {
$profTest[\'TestCaption\'][] = \'Simple var was *not* set\';
for ($i=0; $i<$param; $i++) isSet($sdjf);
}
function isSet3($param, &$profTest) {
$profTest[\'TestCaption\'][] = \'Array var was set\';
$var = array(\'22\'=>TRUE);
for ($i=0; $i<$param; $i++) isSet($var[22]);
}
function isSet4($param, &$profTest) {
$profTest[\'TestCaption\'][] = \'Array var was *not* set\';
$var = array(\'22\'=>TRUE);
for ($i=0; $i<$param; $i++) isSet($var[23]);
}
#----------------------------------------------------------------------
# Is_array test
function isArray1($param, &$profTest) {
$profTest[\'TestCaption\'][] = \'is_array of an array\';
$var = array();
for ($i=0; $i<$param; $i++) is_array($var);
}
function isArray2($param, &$profTest) {
$profTest[\'TestCaption\'][] = \'is_array of a string\';
$var = \'\';
for ($i=0; $i<$param; $i++) is_array($var);
}
function isArray3($param, &$profTest) {
$profTest[\'TestCaption\'][] = \'is_array of a non set value\';
for ($i=0; $i<$param; $i++) is_array($gaga);
}
#----------------------------------------------------------------------
# switch/case vs. if/elseif
function ifOrCase1($param, &$profTest) {
$profTest[\'TestCaption\'][] = \'if and elseif \';
for($c=0; $c<$param; $c++) {
$res = $c % 10;
if ($res == 0) {
} elseif ($res == 1) {
} elseif ($res == 2) {
} elseif ($res == 3) {
} elseif ($res == 4) {
} elseif ($res == 5) {
} elseif ($res == 6) {
} elseif ($res == 7) {
} elseif ($res == 8) {
} else {
}
}
}
function ifOrCase2($param, &$profTest) {
$profTest[\'TestCaption\'][] = \'if and elseif (using ===)\';
for($c=0; $c<$param; $c++) {
$res = $c % 10;
if ($res === 0) {
} elseif ($res === 1) {
} elseif ($res === 2) {
} elseif ($res === 3) {
} elseif ($res === 4) {
} elseif ($res === 5) {
} elseif ($res === 6) {
} elseif ($res === 7) {
} elseif ($res === 8) {
} else {
}
}
}
function ifOrCase3($param, &$profTest) {
$profTest[\'TestCaption\'][] = \'case\';
for($c=0; $c<$param; $c++) {
$res = $c % 10;
switch($res) {
case 0: break;
case 1: break;
case 2: break;
case 3: break;
case 4: break;
case 5: break;
case 6: break;
case 7: break;
case 8: break;
default: break;
}
}
}
#=======================================================================
# Don\'t add anything below this line
#=======================================================================
$out = \'\';
$testSize = sizeOf($profileTest);
for ($i=0; $i<$testSize; $i++) {
$profTest = &$profileTest[$i];
if ($profTest[\'title\'] == $runOnlyThis) continue;
// Now loop through the functions of every test
$funcionNr = 1;
$function = $profTest[\'function\'] . $funcionNr;
while(function_exists($function)) {
$sTime = explode(\' \', microtime());
$function($profTest[\'param\'], $profTest);
$eTime = explode(\' \', microtime());
$profTest[\'result\'][] = (($eTime[1] - $sTime[1]) + ($eTime[0] - $sTime[0]))*1000;
$funcionNr++;
$function = $profTest[\'function\'] . $funcionNr;
}
$out .= \'<hr>\'
. \'<table width=\"95%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\'
. \'<tr align=\"left\" bgcolor=\"#bcd6f1\"><th colspan=2>\'
. $profTest[\'title\']
. \'</th></tr>\'
. \'<tr bgcolor=\"#bcd6f1\"><td colspan=2>\'
. $profTest[\'text\']
. \'</td></tr>\'
;
for ($res=0; $res<sizeOf($profTest[\'result\']); $res++) {
$color = ($res % 2 == 0) ? \'bgcolor=\"#e1fffe\"\' : \'bgcolor=\"#ecf5ff\"\';
$out .= \"<tr {$color}><td width=65% style=\'font-size: 11\'>\";
$funcName = $profTest[\'function\'][$res];
$out .= isSet($profTest[\'TestCaption\'][$res]) ? ($res+1).\': \'. $profTest[\'TestCaption\'][$res] : \' \';
$out .= \'</td><td style=\"font-family: courier\">\';
$time = round($profTest[\'result\'][$res]);
$time = str_pad($time,5,\' \',STR_PAD_LEFT);
$out .= \'Total time:\' . str_replace(\' \',\' \',$time) . \'[ms]\';
$out .= \'</td></tr>\';
}
$out .= \'</table><br>\';
}
echo \'<body style=\"font-family: Verdana, Geneva, Arial\">\';
echo $out;
echo \'</body>\';
?>
Comments or questions?
PX is running PHP 5.2.11
Thanks to Miranda Productions for hosting and bandwidth.
Use of any code from PX is at your own risk.