Page 2 of 2

Re: newbie str_replace question

Posted: Wed Sep 16, 2009 12:36 pm
by jackpf
Yeah...I get str_replace() being faster as well actually. I guess it's because I have to use strlen() as well...I would have thought substr() would be quicker than str_replace() tbh.

My times:
str_replace took 9.1224670410156 seconds
substr took 9.5974769592285 seconds
preg_replace took 13.881380081177 seconds
My code:

Code: Select all

<?php
$begin = array_sum(explode(' ', microtime()));
for($i = 0; $i <= 10000000; $i++)
{
    $str = '$100';
    $str = substr($str, 1, strlen($str));
}
echo sprintf('str_replace took %s seconds', array_sum(explode(' ', microtime())) - $begin);
 
$begin = array_sum(explode(' ', microtime()));
for($i = 0; $i <= 10000000; $i++)
{
    $str = '$100';
    $str = str_replace('$', null, $str);
}
echo sprintf('substr took %s seconds', array_sum(explode(' ', microtime())) - $begin);
 
$begin = array_sum(explode(' ', microtime()));
for($i = 0; $i <= 10000000; $i++)
{
    $str = '$100';
    $str = preg_replace('/\$/', null, $str);
}
echo sprintf('preg_replace took %s seconds', array_sum(explode(' ', microtime())) - $begin);
?>
EDIT
And yeah...you don't actually have to supply substr() with the length of the string...I never knew that. Without strlen(), I get this:
substr without strlen took 5.2599999904633 seconds
So, it was strlen() screwing me over :P Yeah, substr() is waaaay faster.

Re: newbie str_replace question

Posted: Wed Sep 16, 2009 12:37 pm
by jackpf
God damn you guys, you all posted about 6 times when I was editing mine :P

Re: newbie str_replace question

Posted: Wed Sep 16, 2009 12:38 pm
by ricehigh
superdezign: Hehe, i guess not. Because you're absolutely right. I apologize :)

Re: newbie str_replace question

Posted: Wed Sep 16, 2009 12:38 pm
by superdezign
Active forum today. I have class in like 20 mins, so I'm lurking.

Re: newbie str_replace question

Posted: Wed Sep 16, 2009 12:39 pm
by Mirge
ricehigh wrote:superdezign: We are using strlen(), to specify how much of the string we want to substract, that is from the 1st character (remember in programming counting starts from zero), to the total length of the string. Se the php documentation for more info: http://dk.php.net/manual/en/function.substr.php

Edit: actually it seems to perform better without the $length parameter. I honestly wasn't aware of this.

Mirge:
Code i used:
substr()

Code: Select all

<?php
$time = microtime(true);
 
for($x=0;$x<10000000;$x++){
    $str = "$100";
    $str = substr($str, 1, strlen($str));
}
echo $str;
$time = (microtime(true)-$time);
printf('<br/>Page generated in: %0.2f s<br/>',$time);
?>
str_replace():

Code: Select all

<?php
$time = microtime(true);
 
for($x=0;$x<10000000;$x++){
    $str = "$100";
    $str = str_replace('$',"",$str);
}
echo $str;
$time = (microtime(true)-$time);
printf('<br/>Page generated in: %0.2f s<br/>',$time);
?>
In your code you seem to neglect to redefine your string, making substr substract from an empty string.
Furthermore when you neglect to redefine your string you're not testing performance of removing the dollar sign from the string, since you're only removing it at the first iteration.
True, redefined string in for loop. New results (fastest to slowest):

substr (no strlen): 15.927s
ltrim: 17.910s
str_replace: 19.470s
substr (with strlen): 21.940s

Wonder if there's a way to achieve the same result faster??

Re: newbie str_replace question

Posted: Wed Sep 16, 2009 12:42 pm
by jackpf
superdezign wrote:Active forum today. I have class in like 20 mins, so I'm lurking.
yeah, I have home work to do actually :/
Wonder if there's a way to achieve the same result faster??
Nah, substr() owns allll :P

Re: newbie str_replace question

Posted: Wed Sep 16, 2009 12:45 pm
by superdezign
Mirge wrote:Wonder if there's a way to achieve the same result faster??
str_replace() is the fastest of it's kind (assuming substr() isn't considered "of it's kind").

Re: newbie str_replace question

Posted: Wed Sep 16, 2009 12:49 pm
by Mirge
jackpf wrote:
superdezign wrote:Active forum today. I have class in like 20 mins, so I'm lurking.
yeah, I have home work to do actually :/
Wonder if there's a way to achieve the same result faster??
Nah, substr() owns allll :P
Yeah, I'm gonna say substr() is probably the fastest.

On a side note, I never use preg_replace() unless I actually have to do pattern matching. I always prefer str_replace() over it.

Re: newbie str_replace question

Posted: Sun Sep 20, 2009 10:39 am
by tabutcher
Sorry didn't reply earlier.
Just like to thank everyone for their input and to tell you I got it working.

Re: newbie str_replace question

Posted: Sun Sep 20, 2009 2:19 pm
by jackpf
Awesome. You used substr() right? :P

Re: newbie str_replace question

Posted: Tue Sep 22, 2009 9:03 pm
by tabutcher
yep substr() worked a treat