Simple Machines Blogs > Developers' Blog

PHP String Replacement Speed Comparison

<< < (3/6) > >>

winrules:
sprintf: 0.886003017426 seconds
preg_replace: 2.66261482239 seconds
strtr: 31.150698185 seconds
str_replace #1: 1.06948304176 seconds
str_replace #2: 1.39181494713 seconds

Compuart:

--- Quote from: Thantos on June 03, 2007, 03:00:23 PM ---strtr and str_replace both seem to operate the same way.  However, due to strtr's unique properties the results can be very different.
--- End quote ---
Imho the difference between how the two operate is pretty significant. While strtr will iterate through each character of the search string and attempt to replace anything that it can match at the current offset, str_replace iterates through each search/replacement pair and does the replace operation for the whole subject.

The latter may be faster, but having the replaced subjects being searched and replaced again is often undesirable, especially when dealing with variable searches or replacements (like custom smileys).

Oh and strtr is sooooo much better readable ;)

Tristan Perry:
Useful test/script :) My results and setup are thus:

Processor: Intel Xeon CPU 5130 @ 2.00GHz (500.026MHz)
RAM: 512 MB
OS: Linux 2.6.9
PHP version: 4.4.6
Load Averages: 0.83, 0.74, 0.54

sprintf: 0.042463 seconds
preg_replace: 0.573575 seconds
strtr: -0.780915 seconds
str_replace #1: 0.206594 seconds
str_replace #2: 0.520598 seconds

 - not sure why I got a negative time ??? I guess this means that strtr() is SO powerful and quick, that it'll actually sort out your scripts before they need to be run! Awesome! Psychic PHP functions are always fun :P See, strtr is the best function (plus it's really readable, as Compuart said ;))

Thantos:

--- Quote from: Compuart on June 09, 2007, 04:10:34 PM ---
--- Quote from: Thantos on June 03, 2007, 03:00:23 PM ---strtr and str_replace both seem to operate the same way.  However, due to strtr's unique properties the results can be very different.
--- End quote ---
Imho the difference between how the two operate is pretty significant. While strtr will iterate through each character of the search string and attempt to replace anything that it can match at the current offset, str_replace iterates through each search/replacement pair and does the replace operation for the whole subject.

The latter may be faster, but having the replaced subjects being searched and replaced again is often undesirable, especially when dealing with variable searches or replacements (like custom smileys).

Oh and strtr is sooooo much better readable ;)

--- End quote ---
Thats why you use strtr when you need to.  I'm not advocating not using strtr when you need its properties but to use strtr when you want str_replace behavior is bad.  But if you don't need that functionality then why use a slower option?
As for readability it really is minor as you can still define the replacement array the same way and then use array_key() to seperate the from and to.  As show by str_replace #2 it is still faster then strtr.

Daniel15:

--- Quote --- - not sure why I got a negative time
--- End quote ---
Sounds like you're using PHP 4 rather than PHP 5. PHP 5 changed the behaviour of microtime(). Basically, if you pass it "true" as a parameter, it returns the value as a float (PHP 4 returned two values seperated by a space, as does PHP 5 without any parameters).

To make it work properly on PHP 4, add this to the start of the file (below the <?php):

--- Code: ---function microtime_float()
{
    list($usec, $sec) = explode(" ", microtime());
    return ((float)$usec + (float)$sec);
}

--- End code ---

Then, in the script, replace all instances of

microtime(true)

with

microtime_float()

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version