Re: str_replace performance in large mailing script
| From: | David Otton | Date: | Sun, 24 Aug 2003 14:52:07 +0000 |
| Subject: | Re: str_replace performance in large mailing script | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-160586@lists.php.net to get a copy of this message | ||
On Sun, 24 Aug 2003 15:38:13 +0100, you wrote:
>Second tip is that sprintf() should be faster than str_replace(). Compare:
>
>$text = "Hello %s";
>$name = "John";
>
>echo (str_replace ("%s", $name, $text));
>echo (sprintf ($text, $name));
Thinking about it, straight concatenation should be faster than sprintf()
$text = "Hello %s, welcome to the list";
$text = explode ("%s", $text);
echo ($text[0] . $name . $text[1]);
The explode goes outside the main loop, of course. Pre-processing.
The downside is that the simpler (and faster) you make it, the more
assumptions you have to make about the template (in this case, one
substitution per line and the substitution can't be the first or last
element on the line).