Re: preg_replace brain teaser
| From: | SLanger at spirit21 dot de | Date: | Wed, 16 Apr 2003 12:20:45 +0000 |
| Subject: | Re: preg_replace brain teaser | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-144104@lists.php.net to get a copy of this message | ||
Hello
I finally did some testing for preg_replace with /e modifier and
preg_replace_callback using the code below.
For everybody interested the two actually don't differ that much for the
tested string.
Bare in mind that the test depends on the string being parsed, the
optimization the regex engine is doing, the workload on the computer you
are using, etc... so actual results may vary. The relation of the two is
what matters.
Platform is W2K Pentium 3 700MHz PHP 4.3.0 Apache Module with Apache
1.3.2? (don't know this and am too lazy to find out)
The end results are (in Seconds for each run):
no callback: 0.00018804715422789
callback: 0.0001872165110906
Stefan Langer
The test code...
You can uncomment the echos if you are not interested in the results of
the single runs.
<?php
$results = array();
$string = "! {ext}\\{Test} with some e{Test}xtra test that is {curly} not
being replaced. \\\\{to} see if \\{\\{ is being replaced and { too!";
$args = array('ext' => 'extended', 'curly'=>'a curly
bracket', 'Test'=>
'some test');
$pattern1 = '/(?<!\\\\)\\{([a-zA-Z0-9_-]+?)\\}/e';
$pattern2 = '/(?<!\\\\)\\{([a-zA-Z0-9_-]+?)\\}/';
$testString = '';
// make a really long string form the above string
for($i=0; $i<3000; $i++)
{
$testString .= $string;
}
function callback1($match)
{
global $args;
if(isset($args[$match[1]]))
return $args[$match[1]];
return '';
}
function getResult($startTime, $endTime)
{
$startTime = getTime($startTime);
$endTime = getTime($endTime);
return $endTime - $startTime;
}
function getTime($time)
{
list($usec, $sec) = explode(" ",$time);
return ((float)$usec + (float)$sec);
}
$starttime = null;
$endtime = null;
$runLoop = 3000;
// amortize final result by running each test a 1000 times
for($j=0; $j<1000; $j++)
{
echo '<hr>Testing pattern 1 '.htmlentities($pattern1);
$starttime = microtime();
// do replacement a couple of times
for($i=0;$i<$runLoop; $i++)
{
preg_replace($pattern1, '$args[\\1]', $string);
}
$endtime = microtime();
echo '<br>Total time: '.getResult($starttime, $endtime);
array_push($results, getResult($starttime, $endtime)/$runLoop);
echo '<br>Result for '.$runLoop.' runs: ', getResult($starttime,
$endtime)/$runLoop;
echo '<hr>';
echo '<hr>Testing pattern 2 '.htmlentities($pattern2);
$starttime = microtime();
// do replacement a couple of times
for($i=0;$i<$runLoop; $i++)
{
preg_replace($pattern1, '$args[\\1]', $string);
}
$endtime = microtime();
echo '<br>Total time: '.getResult($starttime, $endtime);
array_push($results, getResult($starttime, $endtime)/$runLoop);
echo '<br>Result for '.$runLoop.' runs: ', getResult($starttime,
$endtime)/$runLoop;
echo '<hr>';
}
$count = count($results);
echo '<hr>All ',$count/2,' Results evaluation:';
// calc final results from all 1000 test runs
for($i=0; $i<$count; $i+=2)
{
$final1 += $results[$i];
$final2 += $results[$i+1];
}
echo '<br>Total Time 1: ',2*$final1/$count;
echo '<br>Total Time 2: ',2*$final2/$count;
echo '<hr>';
?>