PHP Benchmark: Multiple str_replace() vs One str_replace() With Array

September 5th, 2012 - Posted by Steve Marks to PHP, Web Development.

I’m a massive fan of optimisation. Inparticular I’m talking about optimising PHP code, rather than, say, HTML code which would improve a users experience. Sometimes however I think it can go too far when it comes to doing something that results in saving just a few milliseconds. In these scenarios, the time taken to perform and code the optimisation is often greater than the total time the optimisation will ultimately save in the long run.

There are times however that I do think optimisation is a worthwhile task:

  • When something (ie a script) is ran thousands, if not millions of times a day
  • When the optimisation will have a positive impact on a servers memory and performance
  • When a script is time sensitive and needs to complete as quickly as possible

In the scenario that led me to perform this benchmark test, it was the first option in the list above that got me initially thinking. One of my scripts contained about 500 individual calls to the PHP function str_replace(), and this script was called hundreds of thousands of times a day. One slight optimisation would have a big impact.

The Question

Which is quicker? Doing hundreds of individual calls to str_replace(), or doing just a few but using an array to store the search and replace values?

The Test

The test would be broken down into two parts. One where just a few replacements happen on a short string just a few words long, and one where lots of replacements would happen on a very long string over 25,000 characters. In both cases we will do lots of singular calls to str_replace() and then just one call to str_replace() but using arrays for the search and replace parameters. Each would be performed in a loop iterating 10,000 times.

To make it fairer the tests would be performed multiple times at the same time of the day on the same server.

A copy of the script used can be found below should you want to run the benchmark for yourself:

Download Benchmark PHP Script

The Results

Short String, Few Replacements

The code for this first test was as follows:

$string = "My cat is furry and likes to eat fish";

## Doing one str_replace() at a time on a short string

$startTime = microtime(true);

for ($i=0; $i<10000; $i++)
{
	$newString = $string;
	$newString = str_replace("cat", "dog", $newString);
	$newString = str_replace("furry", "skinny", $newString);
	$newString = str_replace("likes", "hates", $newString);
	$newString = str_replace("fish", "stones", $newString);
}

$endTime = microtime(true);

echo "Took ".($endTime - $startTime)." microseconds";

## Doing one str_replace() in total using arrays

$search = array("cat", "furry", "likes", "fish");
$replace = array("dog", "skinny", "hates", "stones");

$startTime = microtime(true);

for ($i=0; $i<10000; $i++)
{
	$newString = $string;
	$newString = str_replace($search, $replace, $newString);
}

$endTime = microtime(true);

echo "\nTook ".($endTime - $startTime)." microseconds";

And the results were as follows:

Multiple str_replace()
0.14730405807495 microseconds
0.033537149429321 microseconds
0.033782958984375 microseconds
0.033676147460938 microseconds

One str_replace()
0.026539087295532 microseconds
0.025756120681763 microseconds
0.026238918304443 microseconds
0.026527881622314 microseconds

Long String, Lots Of Replacements

The second test was similar to the first but contained a very long string with five times as many replacements. The code for this test was:

$string = str_repeat("My cat is furry and likes to eat fish. He thinks that rabbits are rubbish and that his owner is brilliant. His name is Alfred and he is 24 years old in August. His hobbies include skiing, archery, swimming and rowing. He does get tired though after all of this exercise. ", 100);

## Doing one str_replace() at a time on a short string

$startTime = microtime(true);

for ($i=0; $i<10000; $i++)
{
	$newString = $string;
	$newString = str_replace("cat", "dog", $newString);
	$newString = str_replace("furry", "skinny", $newString);
	$newString = str_replace("likes", "hates", $newString);
	$newString = str_replace("fish", "stones", $newString);
	$newString = str_replace("thinks", "knows", $newString);
	$newString = str_replace("rabbits", "camels", $newString);
	$newString = str_replace("rubbish", "excellent", $newString);
	$newString = str_replace("brilliant", "exceptional", $newString);
	$newString = str_replace("His", "Her", $newString);
	$newString = str_replace("Alfred", "Alison", $newString);
	$newString = str_replace(" he ", " she ", $newString);
	$newString = str_replace("24", "33", $newString);
	$newString = str_replace("August", "October", $newString);
	$newString = str_replace("hobbies", "interests", $newString);
	$newString = str_replace("skiing", "trains", $newString);
	$newString = str_replace("archery", "aeroplanes", $newString);
	$newString = str_replace("swimming", "motorsport", $newString);
	$newString = str_replace("rowing", "ships", $newString);
	$newString = str_replace("He ", "She ", $newString);
	$newString = str_replace("tired", "excited", $newString);
}

$endTime = microtime(true);

echo "Took ".($endTime - $startTime)." microseconds";

## Doing one str_replace() in total using arrays

$search = array("cat", "furry", "likes", "fish", "thinks", "rabbits", "rubbish", "brilliant", "His", "Alfred", " he ", "24", "August", "hobbies", "skiing", "archery", "swimming", "rowing", "He ", "tired");
$replace = array("dog", "skinny", "hates", "stones", "knows", "camels", "excellent", "exceptional", "Her", "Alison", " she ", "33", "October", "interests", "trains", "aeroplanes", "motorsport", "ships", "She", "excited");

$startTime = microtime(true);

for ($i=0; $i<10000; $i++)
{
	$newString = $string;
	$newString = str_replace($search, $replace, $newString);
}

$endTime = microtime(true);

echo "\nTook ".($endTime - $startTime)." microseconds";

And the results:

Multiple str_replace()
19.818342924118 microseconds
19.381294965744 microseconds
19.562654972076 microseconds
19.654101848602 microseconds

One str_replace()
18.64848780632 microseconds
18.245759010315 microseconds
18.476965904236 microseconds
18.209582090378 microseconds

Conclusion

In both tests it seems apparent that doing a single call to str_replace() is better (or at least quicker) than doing lots of individual calls. The savings are quite small but do build up when we’re talking about this being something that’s done potentially millions of times a day.

This entry was posted on Wednesday, September 5th, 2012 at 8:05 pm by +Steve Marks and is filed under PHP, Web Development. You can follow any responses to this entry through the RSS 2.0 feed.
Comments...

Fear not, we won't publish this

Comments (2)
  1. hyipcore says:

    Nice test, needed this because I have to do large amounts of str_replace and doing it via arrays is obviously the faster way to go.

  2. Hasan says:

    Ow! cool test, i like it… also thanks for the easy example..