For interest, I just ran a completely unscientific speed test for both
styles of template, in particular, I wondered if there is much of an
overhead for PHP switching in and out of <?php ?> tags.
I tried three templates, the nice looking symfony one, one with no
<?php ?> tag switching and a similar amount of whitespace and one with
no <?php ?> tag switching and no whitespace.
When looping through 10000 items, I found that not using <?php ?> tags
in the template is around 20% faster. The difference between the
whitespace and non white space versions was neglible.
Not that I think this is a compelling reason not to use them, since
when dealing with more sensible numbers (say, 250 items in a template
as mentioned), on my dev machine the saving amounts to something like
0.05% of the total page execution time.
Of course, it may vary greatly in your environment :)
Here's the code I ran:
<?php
$myArray = array();
for ($i = 1; $i <= 250; $i++) {
$myArray[$i] = array();
for ($j = 1; $j <= 10; $j++) {
$myArray[$i][] = rand(0, 10);
}
}?>
<?php
ob_start();
$start2 = microtime(true);
echo "<ul>\n";
foreach($myArray as $key => $item) {
echo " <li>Item number $key has entries:\n";
echo " <ul>\n";
foreach($item as $number) {
echo " <li>" . $number;
if ($number > 5) {
echo ' (Greater than 5!)';
}
echo "</li>\n";
}
echo " </ul>\n";
echo " </li>\n";
}
echo "</ul>\n";
$end2 = microtime(true);
ob_end_clean();
?>
<?php
ob_start();
$start3 = microtime(true);
echo '<ul>';
foreach($myArray as $key => $item) {
echo '<li>Item number ' . $key . 'has entries:<ul>';
foreach($item as $number) {
echo '<li>' . $number;
if ($number > 5) {
echo ' (Greater than 5!)';
}
echo '</li>';
}
echo '</ul></li>';
}
echo '</ul>';
$end3 = microtime(true);
ob_end_clean();
?>
<?php
$start1 = microtime(true);
ob_start();
?>
<ul>
<?php foreach($myArray as $key => $item): ?>
<li>Item number <?php echo $key ?> has entries:
<ul>
<?php foreach($item as $number): ?>
<li><?php echo $number ?>
<?php if ($number > 5): ?>
(Greater than 5!)
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
</li>
<?php endforeach; ?>
</ul>
<?php
ob_end_clean();
$end1 = microtime(true);
?>
<?php
$time1 = ($end1 - $start1)*1000;
$time2 = ($end2 - $start2)*1000;
$time3 = ($end3 - $start3)*1000;
echo "Symfony style: " . $time1. "ms<br/>";
echo "Other style, some whitespace: " . $time2 . "ms<br/>";
echo "Other style, no whitespace: " . $time3 . "ms<br/>";
echo "<br/>Other some whitespace is: " . round(100*($time2 -
$time1)/$time1) . "% time taken<br/>";
echo "Other no whitespace is: " . round(100*($time3 - $time1)/$time1)
. "% time taken<br/>";
Ross
2009/1/31 colnector (colnect.com) <[email protected]>:
>
> Thank you all for your insights. On my website I'm already using APC,
> Symfony's cache and mod_gzip.
>
> Mostly an extra space or another <?php ?> block is completely
> negligible. However, sometimes pages becomes heavy (for example, a
> user on my site may see the following list including 250 items:
> http://coins.colnect.com/en/coins/list/country/2504). In such cases, I
> believe the issues I've mentioned need be considered. Also, my site is
> highly dynamic so I cannot cache the really heavy pages (since each
> collector sees the page differently based on his personal collection
> which can change on any click he/she does).
>
> Yes, hardware is getting cheaper and cheaper and code is getting more
> and more bloated.
>
> About readability, I actually have a big problem with Symfony's way of
> adding more <?php ?> blocks. Obviously the example I've included
> didn't demonstrate if but if we add a few IFs and FORs, then consider
> these two:
>
> Symfony style:
> <?php if (true && do_some_function(array(1, 3, $var), $stuff)) : ?>
> <ul>
> <?php foreach($myArray as $item) : ?>
> <li><?php echo link_to($item['display_name'],
> myClass::getItemDisplayModule($item).'/action',
> array('class' => 'some_css_class', 'whatever' =>
> 'gever')); ?></li>
> <?php endforeach; ?>
> </ul>
> <?php endif; ?>
>
> A more optimized PHP script could be:
>
> <?php if (true && do_some_function(array(1, 3, $var), $stuff)) {
> echo '<ul>';
> foreach($myArray as $item) {
> echo '<li>'
> .link_to($item['display_name'], myClass::getItemDisplayModule
> ($item).'/action',
> array('class' => 'some_css_class', 'whatever' => 'gever'))
> .'</li>';
> }
> echo '</ul>';
> } ?>
>
>
> Do you still find the first example more readable?
> >
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"symfony developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/symfony-devs?hl=en
-~----------~----~----~----~------~----~------~--~---