Re: Print or Echo takes lots of time
| From: | Ivo F.A.C. Fokkema | Date: | Thu, 28 Sep 2006 08:23:24 +0000 |
| Subject: | Re: Print or Echo takes lots of time | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-242341@lists.php.net to get a copy of this message | ||
On Tue, 26 Sep 2006 21:45:58 +1000, David Tulloh wrote:
> Google Kreme wrote:
>> On 25 Sep 2006, at 06:11 , Sancar Saran wrote:
>> ...
>>
>> If this is generating hundred of K of HTML, use ' instead of "
>>
>> (yes, it's faster).
>>
>
> I've seen this stated several times and at first glance it seems to make
> sense. The double quoted version has all the \n and related characters
> that need to be handled.
>
> Except that php is a C program and uses the printf family of functions.
> This means that all the single quoted strings actually have to be
> converted to escaped versions of double quoted strings, so internally
> '\n' becomes "\\n".
>
> You can see this in some benchmarks, I ran the following script and a
> single quoted version from the command line. I ran each 10 times,
> interleaved to try to balance changes in the system load.
>
> <?php
> $str="";
> for($i=0; $i<10000000; $i++)
> $str.="foo";
> ?>
>
> I found that the double quoted version averaged 5.5516 seconds against
> the single quoted script of 5.5627. Which really goes to show that
> while the double quoted version is faster the difference is almost
> completely irrelevent.
While we're at it (running benchmarks three times, varying amount of loops):
Benchmarking with 100K loops
Case 1 : 'Adding a 10-character single quoted string to a var'
Time : 0.23s - 0.24s
Case 2 : 'Adding a 10-character double quoted string to a var'
Time : 0.23s - 0.24s
Performance : Anywhere between Decreased 1.1% and Increased 26.7%
This seems really funny to me, cause I was under the impression a single
quoted string was faster... I remember benchmarking that before.
----------------------------------------
Benchmarking with 10K loops
Case 1 : 'Adding a 100-character single quoted string to a var'
Time : 0.03s - 0.08s
Case 2 : 'Adding a 100-character double quoted string to a var'
Time : 0.03s
Performance : Anywhere between Increased 0.7% and Increased 66.9%
Still funny... and numbers get higher, too.
----------------------------------------
Benchmarking with 1M loops
Case 1 : 'Setting a var with a 100-character single quoted string'
Time : 2.29s - 2.63s
Case 2 : 'Setting a var with a 100-character double quoted string'
Time : 2.53 - 2.78s
Performance : Anywhere between Decreased 3.1% and Decreased 16.3%
Now this is probably what I saw previously and what made me decide using
single quotes where possible.
----------------------------------------
Benchmarking with 1M loops
Case 1 : 'Setting a var with a 100-character single quoted string incl. a variable
(concat)'
Time : 3.59s - 4.07s
Case 2 : 'Setting a var with a 100-character double quoted string incl. a variable'
Time : 4.14s - 4.40s
Performance : Anywhere between Decreased 8.2% - Decreased 19.2%
Well, try not to use variables in a string, but concatenate it together.
----------------------------------------
Benchmarking with 1M loops
Case 1 : 'Setting a var with a 100-character double quoted string incl. a variable
(concat)'
Time : 3.58s - 3.79s
Case 2 : 'Setting a var with a 100-character double quoted string incl. a variable'
Time : 4.12s - 5.24s
Performance : Anywhere between Decreased 14.9% - Decreased 38.4%
Same with using double quoted strings only...
----------------------------------------
Hey! It's coffee break already!
Ivo