preg_replace 可以使用正規語法來取代字串任何字元,,今天探討取代空白字元的效能,雖然這是個不起眼的效能評估,一般人不太會這樣去改,不過這是國外 PHP Framework 有人提出來修正的,經過許多人的測試一致同意。功能就是一篇文章內如果有多餘的空白能空取代成一個,一般人都會用 \s+ 正規語法,畢竟大家都知道 \s 代表單一空白或 \r 等符號,但是國外有人提出用 {2,} 方式來取代空白。程式碼如下,大家可以測試看看。
<?php
$nb = 10000;
$str = str_repeat('Hi, I am appleboy ' . "\n", 10);
$t1 = microtime(true);
for ($i = $nb; $i--; ) {
preg_replace('/\s+/', ' ', $str);
}
$t2 = microtime(true);
for ($i = $nb; $i--; ) {
preg_replace('/ {2,}/', ' ',
str_replace(array("\r","\n","\t","\x0B","\x0C"),' ',$str)
);
}
$t3 = microtime(true);
echo $t2 - $t1;
echo "\n";
echo $t3 - $t2;
測試結果(1萬次)
PHP 5.3.3 old: 0.13053798675537 new: 0.058536052703857 PHP 5.3.15 old: 0.11732506752014 new: 0.071418046951294 PHP 5.3.17 old: 0.11612010002136 new: 0.07065486907959 PHP 5.4.5 old: 0.1185781955719 new: 0.066012859344482 PHP 5.4.7 old: 0.11343121528625 new: 0.066931962966919
結論至少快蠻多的,如果整體資料量再大一點,我想差別會更大,那至於要不要用呢,就看個人了 XD。
I love reading through a post that will make people
回覆刪除think. Also, many thanks for allowing me to comment!
my website grep regular expression ()