By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,304 Members | 1,254 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,304 IT Pros & Developers. It's quick & easy.

speed questions

P: n/a
say i was trying to append a % before any occurance of either abc,
abd, or abe, in a string. which would be faster?:

$string = str_replace("abc","%abc",$string);
$string = str_replace("abd","%abd",$string);
$string = str_replace("abe","%abe",$string);

or

$string - preg_replace("/(abc|abd|abe)/","%$1",$string);

php.net says that preg_replace is slower than one str_replace, but for
multiple ones, it seems like it would gain a slight speed advantage.

also, exactly how would the following to code segments compare, speed
wise?

for ($num=0;$num<$length;$num++) {
$array1[] = $num;
$array2[] = $num;
}

for ($num=0;$num<$length;$num++)
$array1[] = $num;
for ($num=0;$num<$length;$num++)
$array2[] = $num;

it seems like the first one should require 3*$length operations,
whereas the first one would require 4*$length operations, although i'm
not 100% sure on that.
Jul 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
yawnmoth wrote:
say i was trying to append a % before any occurance of either abc,
abd, or abe, in a string.**which*would*be*faster?:

$string = str_replace("abc","%abc",$string);
$string = str_replace("abd","%abd",$string);
$string = str_replace("abe","%abe",$string);

or

$string - preg_replace("/(abc|abd|abe)/","%$1",$string);


Write a test script and run it in a loop say 1000 times doing the firstand
then the second and measure it. That'll tell you which is faster.

--
Chris Hope - The Electric Toolbox - http://www.electrictoolbox.com/
Jul 17 '05 #2

P: n/a
"yawnmoth" <te*******@yahoo.com> wrote in message
news:7j********************************@4ax.com...
say i was trying to append a % before any occurance of either abc,
abd, or abe, in a string. which would be faster?:

$string = str_replace("abc","%abc",$string);
$string = str_replace("abd","%abd",$string);
$string = str_replace("abe","%abe",$string);

or

$string - preg_replace("/(abc|abd|abe)/","%$1",$string);

php.net says that preg_replace is slower than one str_replace, but for
multiple ones, it seems like it would gain a slight speed advantage.


you can also call str_replace with arrays of strings as parameters.

$string = str_replace(array("abc","abd","abe") ,
array("%abc","%abd","%abe"), $string);

rush
--
http://www.templatetamer.com/
Jul 17 '05 #3

P: n/a
yawnmoth wrote:
say i was trying to append a % before any occurance of either abc,
abd, or abe, in a string. which would be faster?:

$string = str_replace("abc","%abc",$string);
$string = str_replace("abd","%abd",$string);
$string = str_replace("abe","%abe",$string);

or

$string - preg_replace("/(abc|abd|abe)/","%$1",$string);
A correct preg_replace will be the fastest.
$string = preg_replace("/(ab[cde])/","%$1",$string);

This will be a bit slower than one str_replace, and a lot faster than 3
of them.

also, exactly how would the following to code segments compare, speed
wise?

for ($num=0;$num<$length;$num++) {
$array1[] = $num;
$array2[] = $num;
}

for ($num=0;$num<$length;$num++)
$array1[] = $num;
for ($num=0;$num<$length;$num++)
$array2[] = $num;

it seems like the first one should require 3*$length operations,
whereas the first one would require 4*$length operations, although i'm
not 100% sure on that.


fastest here:
$array1 = $array2 = range(0,$length);

;)

greetings, Christian.
Jul 17 '05 #4

P: n/a
"yawnmoth" <te*******@yahoo.com> wrote in message
news:7j********************************@4ax.com...
say i was trying to append a % before any occurance of either abc,
abd, or abe, in a string. which would be faster?:

$string = str_replace("abc","%abc",$string);
$string = str_replace("abd","%abd",$string);
$string = str_replace("abe","%abe",$string);

or

$string - preg_replace("/(abc|abd|abe)/","%$1",$string);

php.net says that preg_replace is slower than one str_replace, but for
multiple ones, it seems like it would gain a slight speed advantage.
I always like strtr(). Looks neater and doesn't involve the regexp engine.

$table = array(
"abc" => "%abc",
"abd" => "%abd",
"abe" => "%abe"
);
$string = strtr($string, $table);
also, exactly how would the following to code segments compare, speed
wise?

for ($num=0;$num<$length;$num++) {
$array1[] = $num;
$array2[] = $num;
}

for ($num=0;$num<$length;$num++)
$array1[] = $num;
for ($num=0;$num<$length;$num++)
$array2[] = $num;


The correct answer is $array1 = $array2 = range(0, $length - 1);
Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.