Authentication




Cancel OK
B!コメントする  2013-01-15 09:51:00 by old

文字列置き換え比較(preg_replace/str_replace)

単純にどっちが早いのかと思ったので1分ほどで以下のものを書いてみた

$target = '
abcdefg

hijklmn

opqrstu

vwxyz
';

// 開始
$timestart = microtime(true);
for( $i = 0; $i < 10000; $i++ )
{
$result = preg
replace("/\n\n/", "", $target);
}
// 終了
$time = microtime(true) - $timestart;
// 結果
echo "preg
rplace " . $time . " second\n";


// 開始
$timestart = microtime(true);
for( $i = 0; $i < 10000; $i++ )
{
$result2 = str
replace("\n\n", "", $target);
}
// 終了
$time2 = microtime(true) - $timestart;
// 結果
echo "str
replace " . $time2 . " second\n";


単純に空行を削除するだけですが結果は以下の通り


pregrplace 0.016462802887 second
str
replace 0.00956201553345 second


まぁ当たり前といえば当たり前でしょうか。
正規表現を見るのと単なる一致じゃロジックが違うだろうし。

正規表現が必要ないときは置き換えは「str_replace」使えってhttp://php.net/manual/ja/function.str-replace.phpにも書いてあるし。


PHP  

  • コメント
  • コメントはまだありません