Why Smarty is too slow?
Smarty Template Engine is the most popular and widely used template engine for PHP. It is believed that Smarty is fast because it compiles templates into PHP code, but it is not true.
I have tried a simple benchmark program to measure speed of Smarty and PHP include() function. The result shows that PHP include() function is more than three times faster than Smarty.
Why Smarty is so slow? Because the PHP code compiled by Smarty is not efficient. The following example shows Smarty template code and compiled PHP code.
// Smarty template
<ul>
{foreach from=$list item=item}
<li>{$item|escape}</li>
{/foreach}
</ul>
// PHP code compiled by Smarty
<ul>
<?php $_from = $this->_tpl_vars['list'];
if (!is_array($_from) && !is_object($_from)) {
settype($_from, 'array');
}
if (count($_from)):
foreach ($_from as $this->_tpl_vars['item']):
?>
<li><?php echo ((is_array($_tmp=$this->_tpl_vars['item']))
? $this->_run_mod_handler('escape', true, $_tmp)
: smarty_modifier_escape($_tmp)); ?>
</li>
<?php endforeach;
endif;
unset($_from);
?>
</ul>
This shows that compiled code by Smarty is too complex and not efficient. This is why Smarty is too slow.