newbie str_replace question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: newbie str_replace question

Post 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.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: newbie str_replace question

Post by jackpf »

God damn you guys, you all posted about 6 times when I was editing mine :P
ricehigh
Forum Newbie
Posts: 21
Joined: Mon Sep 14, 2009 5:18 pm

Re: newbie str_replace question

Post by ricehigh »

superdezign: Hehe, i guess not. Because you're absolutely right. I apologize :)
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: newbie str_replace question

Post by superdezign »

Active forum today. I have class in like 20 mins, so I'm lurking.
User avatar
Mirge
Forum Contributor
Posts: 298
Joined: Thu Sep 03, 2009 11:39 pm

Re: newbie str_replace question

Post 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??
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: newbie str_replace question

Post 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
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: newbie str_replace question

Post 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").
User avatar
Mirge
Forum Contributor
Posts: 298
Joined: Thu Sep 03, 2009 11:39 pm

Re: newbie str_replace question

Post 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.
tabutcher
Forum Newbie
Posts: 20
Joined: Fri Aug 21, 2009 7:10 am

Re: newbie str_replace question

Post by tabutcher »

Sorry didn't reply earlier.
Just like to thank everyone for their input and to tell you I got it working.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: newbie str_replace question

Post by jackpf »

Awesome. You used substr() right? :P
tabutcher
Forum Newbie
Posts: 20
Joined: Fri Aug 21, 2009 7:10 am

Re: newbie str_replace question

Post by tabutcher »

yep substr() worked a treat
Post Reply