Re[2]: [PHP] preg_replace brain teaser

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

« previous php.general (#144106) next »