Home: PHP Diary | Script School | PHP Scripts | TD Scripts.com
TD Forum - An unthreaded messageboard script to exchange ideas with your site visitors


[back]
go back 01/13/03 "PEAR, benchmarking str_replace, preg_replace & ereg_replace" go forward[next]

PEAR

PEAR is out of beta now and I visited the website to check it out. I'm going to stop using the shorthand <? and ?> and switch to the more universal <?php and ?> which fits with the PEAR conventions. They list this convention as being: "...the most portable way to include PHP code on differing operating systems and setups."

Benchmarking str_replace and preg_replace 

When to use one function over the other?

I was visiting a few PHP forums and seeing some code passed around. I saw one suggestion for how to remove the extraneous characters in a phone number. The suggestion by one respondent was to use the following:

VERSION 1 - using 5 str_replace function calls:

$phone "555-174-3268";

$phone str_replace("-"""$phone);
$phone str_replace("."""$phone);
$phone str_replace(" """$phone);
$phone str_replace("("""$phone);
$phone str_replace(")"""$phone);

str_replace: AVERAGE Benchmark VERSION 1 (100 iterations): 0.0001228000

I started to type in that I wondered if using one preg_replace wouldn't be more efficient and then a voice chimed in my head: "benchmark it and find out!" It is already known that str_replace is faster than using a regular expression, but if you have to call the function 5 times instead of 1 preg_replace function is it really?

Only way really to find these things out is to benchmark your code. Here is TEST 2:

VERSION 1 - using 1 preg_replace call:

$phone "555-174-3268";
$phone
 preg_replace("/[\. \(\)\-]/"""$phone);

preg_replace: AVERAGE Benchmark VERSION 2 (100 iterations): 0.0000792000

The results of the test: 

Percent (Winner: TEST 2 -- preg_replace): 86.99%

This illustrates that using a single preg_replace in place of multiple str_replace will be about 14% faster. But the next logical question is where becomes the cutoff? Next I ran the test with 2 str_replace function calls like this:

VERSION 1 - using 2 str_replace function calls:

$phone "555-174-3268";

$phone str_replace("-"""$phone);
$phone str_replace("."""$phone);

Versus this:

VERSION 2 - using 1 preg_replace call for two replacements:

$phone "555-174-3268";
$phone
 preg_replace("/[\.\-]/"""$phone);

The results of this test: 

Percent (Winner: TEST #2 -- str_replace): 84.58%

Hmm, so it would seem that the cutoff is somewhere between 3 and 4 function calls? I tried with 4 function calls next:

Percent (Winner: TEST #3 -- preg_replace/str_replace (varies): 99.24%

The bottom line of this study is that I learned that if I need to use 4 or more str_replace functions, I should use preg_replace instead. I try and benchmark my code in the development cycle and when I see repetition.

BTW if you are using the older ereg_replace in your code, it's pretty well known these days on the PHP mailing list and in other places (like the PHP manual) that this function is slower than both newer preg_replace and str_replace. I did a test benchmark for str_replace with 5 function calls versus ereg_replace and the result was:

(str_replace) AVERAGE TEST 1 (100 iterations): 0.0000507500
(ereg_replace) AVERAGE TEST 2 (100 iterations): 0.0000830000

Percent (Winner: TEST #4 -- str_replace): 61.14% (38.96% faster!)

Yikes! This is fair warning to start swapping out your ereg_replace calls in your code, especially when efficiency matters.

Happy coding to you!

Please vote on the usefulness of this diary entry so other people will know if it is worth their time to read :)

How useful was this diary entry? Avg Surfer Rating: 4.06 (249)

New forum for discussion of diary entries

The homeroom at Script School is available to discuss this and other php-scripts.com diary entries. You must be an enrolled student at Script School to add comments to these diary entries.

[back]go back 01/12/03 "PEAR, benchmarking str_replace, preg_replace & ereg_replace" go forward[next]

Home: PHP Diary | Script School | PHP Scripts | TD Scripts.com

Copyright 1999-2003 php-scripts.com Last Modified 01/13/03 12:22