Re: Re: Newbie question about <?= ?>

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 




----- Original Message ----- From: "Jon Anderson" <jon@xxxxxxxxxxxxxxxxxx>
To: <php-general@xxxxxxxxxxxxx>
Cc: "Al" <news@xxxxxxxxxxxxx>
Sent: Sunday, September 10, 2006 9:16 PM
Subject: Re:  Re: Newbie question about <?= ?>


Al wrote:
Structurally, there is a far better way to compile your html pages. This approach is easier to design and debug and it is faster since it sends one complete packet instead of one for every short tag. And, it saves using ob_start() and ob_flush().

Consider:

$report= '';

$report .= function() [or whatever]

..... repeat as necessary to assemble your complete page.

Then simply

echo $report;
I thought I'd look into this, because I'm a bit of a performance nut - I like my code to run as fast as possible at all times. I wrote up a quick buffer v.s. direct "benchmark" for this, and the winner is clear: direct output is much faster. (If my example below isn't what you meant, please let me know. I'm always happy to hear new ways to improve my code.)

Best of 3 runs with apache bench (concurrency 10, 1000 requests total):
Direct output: 582 requests a second
Buffer var: 286 requests a second

I believe the margin would get wider with real-world usage, as the buffer variable would increase in size. My test code is copied below.

jon

--- "Direct output": testecho.php ---

<html>
<head>
<style type="text/wastespacetosimulateastylesheet">
style1 {
   a = 1;
   b = 2;
   c = 3;
   d = 4;
   e = 5;
   f = 6;
}
style2 {
   a = 1;
   b = 2;
   c = 3;
   d = 4;
   e = 5;
   f = 6;
}
style3 {
   a = 1;
   b = 2;
   c = 3;
   d = 4;
   e = 5;
   f = 6;
}
style4 {
   a = 1;
   b = 2;
   c = 3;
   d = 4;
   e = 5;
   f = 6;
}
</style>
</head>
<body><table>

<?php for ($x=0;$x<1000;$x++) { ?>
   <tr><td>X is <?= $x ?></td></tr>
<?php } ?>

</table></body>
</html>

--- "Buffered output": testbuffer.php ---

<?php

$buffer = '
<html>
<head>
<style type="text/wastespacetosimulateastylesheet">
style1 {
   a = 1;
   b = 2;
   c = 3;
   d = 4;
   e = 5;
   f = 6;
}
style2 {
   a = 1;
   b = 2;
   c = 3;
   d = 4;
   e = 5;
   f = 6;
}
style3 {
   a = 1;
   b = 2;
   c = 3;
   d = 4;
   e = 5;
   f = 6;
}
style4 {
   a = 1;
   b = 2;
   c = 3;
   d = 4;
   e = 5;
   f = 6;
}
</style>
</head>
<body><table>';

for ($x=0;$x<1000;$x++) {
   $buffer .= "<tr><td>X is $x</td></tr>";
}

$buffer .= '</table></body>
</html>';

echo $buffer;
?>

--
In my message I was careful to mention that stepping in and out of PHP was good 'for the longer invariable parts of immutable HTML'. What could be considered 'longer' is certainly a matter discussion, your results prove that this is not long enough. Notice that when the parser finds the '<?=', it converts it into the equivalent of "<? echo", thus, though the echo is not explicitly there, from the parser on is as if it were. Then, since you have an echo, why not use it for all of the output? The equivalent to what I showed as the second best, which would be the first best with 'shorter' strings would be the following:

for ($x=0;$x<1000;$x++) {
   echo ' <tr><td>X is ' , $x , '</td></tr>';
}

Can you try and time that one so we have comparable results? This one should be second best:

for ($x=0;$x<1000;$x++) {
   echo "<tr><td>X is $x</td></tr>";
}

Back again to what would be 'longer', well, in your example, the whole header, up to the loop itself should be faster if sent out of PHP. Likewise, you could echo $buffer right after the loop, drop out of PHP and send the footer as plain HTML. This, of course, is harder to time since it happens only once. I admit though that I did time the options I listed and on the 'dropping in and out of PHP' I'm relying on the PHP manual ( see http://www.php.net/manual/en/language.basic-syntax.php, the first paragraph after the examples) and the source of the lexical scanner, which supports that, though your numbers do contradict it. Interesting.

Satyam

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


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux