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

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

 



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;
?>

--
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