PHP function speed testing
| From: | Shawn McKenzie | Date: | Sun, 25 Jul 2004 22:21:52 +0000 |
| Subject: | PHP function speed testing | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-191844@lists.php.net to get a copy of this message | ||
O.K. So I've written my application and debugged it, and now I want to try and optimize the speed. I have written a little test script to test the time of each PHP function that I can use in a certain situation (ereg, eregi, strpos). Problem is, no matter what I do, the first function I test in the script is always the slowest, even if I reorder them so that strpos() is first instead of ereg(). Doesn't matter which one it is. Maybe I did something stupid and just can't see it? Below is the code:
// define the stuff to use
$GLOBALS['renderstart'] = 0;
$key = "testkey";
$teststring = "test500";
$test = array(
'testkey' =>
array (
'test50' =>
array (
'main' => 'admin.html',
),
'test500' =>
array (
'main' => 'admin.html',
),
'default' =>
array (
'main' => 'admin.html',
),
),
);
// functions
function start_timer()
{
$mtime = explode(" ", microtime());
$GLOBALS['renderstart'] = $mtime[1] + $mtime[0];
}
function stop_timer()
{
$mtime = explode(" ", microtime());
$renderstop = $mtime[1] + $mtime[0];
$rendertime = ($renderstop - $GLOBALS['renderstart']);
printf(" [ %f seconds ]<br>\n", $rendertime);
}
// test code
$matchlen = 0;
reset($test[$key]);
start_timer();
foreach ($test[$key] as $ops => $vals) {
if ((strlen($ops) > $matchlen) && ereg($ops, $teststring)) {
$modops = $ops;
$matchlen = strlen($modops);
}
}
echo "ereg";
stop_timer();
$matchlen = 0;
reset($test[$key]);
start_timer();
foreach ($test[$key] as $ops => $vals) {
if ((strlen($ops) > $matchlen) && eregi($ops, $teststring)) {
$modops = $ops;
$matchlen = strlen($modops);
}
}
echo "eregi";
stop_timer();
$matchlen = 0;
reset($test[$key]);
start_timer();
foreach ($test[$key] as $ops => $vals) {
if ((strlen($ops) > $matchlen) && ereg($ops, $teststring)) {
$modops = $ops;
$matchlen = strlen($modops);
}
}
echo "strpos";
stop_timer();
Any ideas???
Thanks!
-Shawn