On Thu, 30 Jan 2003 06:48, Ilia A. wrote:
> > I may be wrong since I haven't profiled this, but my understanding is
> > that str_replace is much faster than doing either of the regex
> > replacements.  For that reason alone, there is a use for it.
>
> Normally it would be quite faster, however once case sensitivity is added
> to the mix I believe the speed difference would be minimal. I've done some
> benchmarks and the speed difference between str_replace() and
> preg_replace() is only about 1/2 second in across 100000 executions (5
> replaces per instance). Another .6 of a second is added when preg_replace()
> is used with 'i' flag which makes it case insensitive. I have not
> benchmarked the stri_replace code but I imagine it would be in the same
> ballpark, meaning that we are adding a fairly large chunk of code for
> performance benefit of a 1 microsecond (1 millionth of a second) per
> instance.
>
> Ilia

Lies, damn lies and statistics.

You say the difference is only 1/2 second accross 100000 executions, but that 
means nothing unless you put it in context of how long the 100000 executions 
took. 1/2 second could mean 90% difference or 1% differene.

I wrote a very unscientific script to do a simple benchmark

<?php
include('stopwatch.inc');
$SW = new StopWatch;
for ($i=0;$i<500000;$i++)
        str_replace('abcdefgh', 'def', 
'fkjdals;fjdsakl;fjdsakl;fjdskl;fadabcdefghfdsafdsafdsa');
$SW->Stop();

for ($i=0;$i<500000;$i++)
        preg_replace('/abcdefgh/', 'def', 
'fkjdals;fjdsakl;fjdsakl;fjdskl;fadabcdefghfdsafdsafdsa');
$SW->Stop();
?>

I did quite a few runs and picked the upper and lower end of the results to 
paste here 

Biggest difference
Total Time: 00:00:03.00
Total Time: 00:00:08.90

Smallest difference
Total Time: 00:00:03.12
Total Time: 00:00:06.94

Bearing in mind this is on a pre-working-hours quad hyperthreaded 1.4ghz xeon 
box. So I've got a cpu just about all to myself here.

Regards,
Stephen Thorne.

-- 
PHP Development Mailing List <http://www.php.net/>
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to