Re: [PHP] Re: optimilize web page loading



Eric Butera wrote:
On Thu, Mar 27, 2008 at 12:41 PM, Peter Ford <pete@xxxxxxxxxxxxx> wrote:
Jason Pruim wrote:
>
> On Mar 27, 2008, at 11:05 AM, Shawn McKenzie wrote:
>> Al wrote:
>>> Good point. I usually do use the single quotes, just happened to key
>>> doubles for the email.
>>>
>>> Actually, it's good idea for all variable assignments.
>>>
>>> Philip Thompson wrote:
>>>> On Mar 26, 2008, at 6:28 PM, Al wrote:
>>>>> Depends on the server and it's load. I've strung together some
>>>>> rather large html strings and they aways take far less time than the
>>>>> transient time on the internet. I used to use OB extensively until
>>>>> one day I took the time to measure the difference. I don't recall the
>>>>> numbers; but, I do recall it was not worth the slight extra trouble
>>>>> to use OB.
>>>>>
>>>>> Now, I simple assemble by html strings with $report .= "foo"; And
>>>>> then echo $report at the end. It also makes the code very easy to
>>>>> read and follow.
>>>>
>>>> You might as well take it a step further. Change the above to:
>>>>
>>>> $report .= 'foo';
>>>>
>>>> This way for literal strings, the PHP parser doesn't have to evaluate
>>>> this string to determine if anything needs to be translated (e.g.,
>>>> $report .= "I like to $foo"). A minimal speedup, but nonetheless...
>>>>
>>>> ~Philip
>>>>
>>>>
>>>>> Andrew Ballard wrote:
>>>>>> On Wed, Mar 26, 2008 at 1:18 PM, Al <news@xxxxxxxxxxxxx> wrote:
>>>>>>> You are really asking an HTML question, if you think about it.
>>>>>>>
>>>>>>> At the PHP level, either use output buffering or assemble all your
>>>>>>> html string as a variable and
>>>>>>> then echo it. The goal is to compress the string into the minimum
>>>>>>> number of packets.
>>>>>> Yes, but do so smartly. Excessive string concatenation can slow
>>>>>> things
>>>>>> down as well. On most pages you probably won't notice much
>>>>>> difference,
>>>>>> but I have seen instances where the difference was painfully obvious.
>>>>>> Andrew
>>
>> Yes and if your script takes .00000000000000000000000000000002 seconds
>> to run using double quotes it will only take
>> .000000000000000000000000000000019 seconds with single (depending upon
>> how many quotes you have of course) :-)
>
> I'm coming in late to this thread so sorry if I missed this :)
>
> How much of a difference would it make if you have something like this:
> echo "$foo bar bar bar bar $foo $foo"; verses: echo $foo . "bar bar bar
> bar" . $foo $foo; ?In other words... You have a large application which
> is most likely to be faster? :)
>
>

There was a discussion about this a few weeks ago - ISTR that the compiler does
wierd things with double-quoted strings, something like tokenising the words and
checking each bit for lurking variables.
So in fact


echo "$foo bar bar bar bar $foo $foo";

is slowest (because there *are* variables to interpolate,


echo $foo . " bar bar bar bar ".$foo." ".$foo;

is a bit faster, but the double-quoted bits cause some slow-down,


echo $foo . ' bar bar bar bar '.$foo.' '.$foo;

is a bit faster again - the single quoted bits pass through without further
inspection, and finally


echo $foo,' bar bar bar bar ',$foo,' ',$foo;

is actually the fastest, because the strings are not concatenated before output.

I think that was the overall summary - I can't locate the original post to
verify (or attribute) but it's in this list somewhere...

Cheers

--
Peter Ford phone: 01580 893333
Developer fax: 01580 893399
Justcroft International Ltd., Staplehurst, Kent



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Can you prove these statements with real benchmarks that are current?
Ilia said that it is a myth that there is a performance difference
between " and ' in one of his talks.

I found one recent post on the subject in gmane:
http://article.gmane.org/gmane.comp.php.general/169028




The poster's results are as I remembered, except that was building a string, not echoing: possibly not quite the same.

So I tried it myself, adapting to using echo (to the ob to avaoid printing forty million * "foo bar bar ..."). For an extra wheeze, I also tried <?=$foo?> syntax embedded in HTML to see if that is as bad as people make out. I didn't try HEREDOC syntax, because it is difficult to reproduce exactly the same output - newlines get added to the output.
To get thing to run I needed to increase time out, and reduce the string to avoid filling the PHP memory limit I have set at the moment. Here is the code:

<?php
ini_set('max_execution_time',300);
$foo = 'f';
ob_start();
$time[1] = microtime(TRUE);
for($x = 0; $x < 10000000; $x++){
echo "$foo b b b b $foo $foo";
}
$time[2] = microtime(TRUE);
ob_end_clean();
ob_start();
$time[3] = microtime(TRUE);
for($x = 0; $x < 10000000; $x++){
echo $foo . ' b b b b '.$foo.' '.$foo;
}
$time[4] = microtime(TRUE);
ob_end_clean();
ob_start();
$time[5] = microtime(TRUE);
for($x = 0; $x < 10000000; $x++){
echo $foo . " b b b b ".$foo." ".$foo;
}
$time[6] = microtime(TRUE);
ob_end_clean();
ob_start();
$time[7] = microtime(TRUE);
for($x = 0; $x < 10000000; $x++){
echo $foo,' b b b b ',$foo,' ',$foo;
}
$time[8] = microtime(TRUE);
ob_end_clean();
ob_start();
$time[9] = microtime(TRUE);
for($x = 0; $x < 10000000; $x++){
?><?=$foo?> b b b b <?=$foo?> <?=$foo?><?php
}
$time[10] = microtime(TRUE);
ob_end_clean();
echo 'For 10,000,000 loops:';
echo '<br />Interpolation:',($time[2]-$time[1]);
echo '<br />Concatenation single quote:',($time[6]-$time[5]);
echo '<br />Concatenation double quote:',($time[4]-$time[3]);
echo '<br />List:',($time[8]-$time[7]);
?>

The results are slightly different to what I expected:

For 10,000,000 loops:

Interpolation:13.385520935059

Concatenation single quote:14.523960828781

Concatenation double quote:14.472594976425

List:11.083581924438

Embedded in HTML:16.329668045044

So, it looks like for echoing at least then single quotes are actually marginally slower than double quotes, and interpolation is faster.
Embedding in HTML *is* slower, and the comma-separated list syntax is fastest.

I suspect YMMV...


--
Peter Ford phone: 01580 893333
Developer fax: 01580 893399
Justcroft International Ltd., Staplehurst, Kent
.



Relevant Pages

  • Re: [PHP] Re: optimilize web page loading
    ... doubles for the email. ... then echo $report at the end. ... This way for literal strings, the PHP parser doesn't have to evaluate ... echo "$foo bar bar bar bar $foo $foo"; ...
    (php.general)
  • Re: MySql count
    ... foreach ($arr as $client) { ... echo "$client has $count"; ... You can't pass variables within single quotes. ... Strings in MySQL queries need to be surrounded by quotes (either single or ...
    (comp.lang.php)
  • Re: Dynamic directory handles?
    ... Cost to develop code that saves those cycles: ... I use single quotes unless I require one of the two extra ... Some strings contain variables, some strings don't. ...
    (comp.lang.perl.misc)
  • Re: heredoc and array problems
    ... When you echo an array variable such as $Din SINGLE QUOTES ... you need to concatenate it as single quotes means a string literal ... So the implied interpolation works only inside strings. ...
    (comp.lang.php)
  • Re: Handling Strings in SQL server
    ... First, if your application has problems with the "O'Malley" issue (imbedded single quotes in strings), you need to address the problem at the root. ... Consider than if your code permits users to enter strings with embedded single quotes, they can also introduce SQL injection attacks. ...
    (microsoft.public.dotnet.framework.adonet)