Re: how to post a question?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Ryan A wrote:
Hey Rich,


The str_replace calls are kinda expensive IMHO, a good regex would be
better


I'd be interested to see a benchmark (any benchmark) where str_replace is
SLOWER than the corresponding Regex...


I am not very good with RegEx's myself, and I have never benchmarked any of
them but
I was told or read (when i first started learning php) that using multiple
str_replace()s are
not a good idea and to use a regex instead.
(Thats why I wrote the "IMHO",if i was wrong one of the gurus here could
correct me)

It kind of makes sense, for example using this from the php site:

$phrase  = "You should eat fruits, vegetables, and fiber every day.";
$healthy = array("fruits", "vegetables", "fiber");
$yummy  = array("pizza", "beer", "ice cream");

$newphrase = str_replace($healthy, $yummy, $phrase);

in the above, first it searches the entire phrase for the first parameter
(fruits) then it starts
again fro the second parameter (vegetables) etc, if you have a really long
text it could
be quite process intensive....because it would have to go through the whole
text searching for
the parameters one by one....right?

You're the guy who usually helps me (and a whole lot of others) when we have
questions

isn't he just! :-) I have to wonder how long his keyboards last ;-), or
whether he retro-fits them with smoke alarms (speedtyping -> overheating ->
fire)

so I am really interested on your view on this, and why your view favours
one side more than
the other.

regexps require a complete engine (in the same way as PHP is a engine)
in order to be parsed. loading/initializing such an engine is heavier than doing a 'simple'
search and replace action.... performance results (between str_replace() and pre_replace()) will
definitely be skewed when comparing simple regexps against a comparable str_replace()
because (to start with) the overhead of calling a function in php (which is why its better
to call str_replace() with arrays in param 1 and 2 rather than calling str_replace()
multiple times)

...also sporadic CPU load due to totally unrelated processes (even when running on
your local machine) can totally skew a benchmark test.

I think you can trust Richard on this one!
and if you don't ... :-) ... then I bet you a beer he'd say "we'll then, why not
test it and see!"




Cheers,
Ryan


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux