explain these testresults (search/replace)

From: Date: Tue, 13 Nov 2001 19:43:20 +0000
Subject: explain these testresults (search/replace)
Groups: php.general 
Request: Send a blank email to php-general+get-74486@lists.php.net to get a copy of this message
1.7303379774094 seconds with strtr() 1.557923078537 seconds with preg_replace() 1.494078040123 seconds with str_replace() 1.4984160661697 seconds with ereg_replace() /* source -------8<-------*/ <?php $files = array("html1.html", "html2.html"); $strtr_rep = array( "<pre>" => "PRE", "<li>"=>"LI", "<tr" => "TABLE ROW START", "<b>" => "BOLD", "<td" => "TABLE CELL START" ); /* ----- END CONFIG ------ */ // read in filedata for ($i = 0; $i < count($files); $i++) { $raw[] = implode("", file($files[$i])); } // build preg_replace reg/replace pattern arrays while(list($reg, $rep) = each($strtr_rep)) { $preg_reg[] = "/".$reg."/"; $preg_repl[] = $rep; } //START TEST $timer = new Timer(); for ($i=0; $i < 1000; $i++) { $data = ($i % 2 == 0) ? $raw[0] : $raw[1]; if ($test == 1) { $test = "strtr"; $data = strtr($data, $strtr_rep); } elseif ($test == 2) { $test = "preg_replace"; $data = preg_replace($preg_reg, $preg_repl, $data); } elseif ($test == 3) { $test = "str_replace"; while(list($k, $v) = each($strtr_rep)) { $data = str_replace($k, $v, $data); } } elseif ($test == 4) { $test = "ereg_replace"; while(list($k, $v) = each($strtr_rep)) { $data = ereg_replace($k, $v, $data); } } } print $timer->getElapsed() . " seconds with $test"; ?> /* -------8<--------- END SOURCE -----8<------8<----- */ -- Best Regards Niklas Fondberg - niklas.fondberg@i3micro.org System Developer - I3 Micro Technology

« previous php.general (#74486) next »