Smarty vs. Twig: performance
Twig - a young template engine from Symfony developers. Authors position it as a fast and functional template engine. In terms of capabilities, it is largely similar to Smarty 3. Twig has a slightly different syntax, as well as the declared high performance. Check it out?
Testing
During testing, we will intentionally use complex enough templates so that the processing time is noticeable. Actually, this time we will evaluate, for which we will prepare the appropriate scripts.
The code for Smarty is very simple:
$data = json_decode(file_get_contents('data.json'), true);
require('smarty/Smarty.class.php');
$smarty = new Smarty();
$smarty->compile_check = false;
$start = microtime(true);
$smarty->assign($data);
$smarty->fetch('demo.tpl');
echo microtime(true)-$start;
Twig is a bit trickier:
$data = json_decode(file_get_contents('data.json'), true);
require('twig/Autoloader.php');
Twig_Autoloader::register();
$loader = new Twig_Loader_Filesystem('templates');
$twig = new Twig_Environment($loader, array(
'cache' => 'templates_c',
'autoescape' => false,
'auto_reload' => false,
));
$start = microtime(true);
$template = $twig->loadTemplate('demo.tpl');
$template->render($data);
echo microtime(true)-$start;
Both template engines are configured in a similar way: automatic screening is turned off, automatic template recompilation is disabled when changes are made.
Retrieving Variable Values
Getting variable values is the most actively used operation. In complex templates, it can be used several hundred times. At first, it might seem that the speed of this operation should not depend on the template engine, but this is not so. Some structure must be used to store the template variables. The simpler it is, the faster the process of obtaining variable values. To evaluate the performance of this operation, we generate a template in which the value of 10,000 variables will simply be displayed and nothing more.
Smarty:
{$var0} {$var1} {$var2} {$var3} {$var4} ...Twig:
{{ var0 }} {{ var1 }} {{ var2 }} {{ var3 }} {{ var4 }} ...Result:
| Compilation | Performance | |
|---|---|---|
| Smarty 3.1.1 | 16.320 sec. | 0.058 seconds |
| Twig 1.2.0 | 9.757 sec | 0.083 sec |
Traversing arrays and displaying field values
No more or less serious template can do without foreach. For the test, we will write a template that displays 10 fields of 1000 elements of the array.
Smarty:
{foreach $array as $item}
{$item.id} {$item.title} {$item.var1} {$item.var2} {$item.var3} {$item.var4} {$item.var5} {$item.var6} {$item.var5} {$item.var6}
{/foreach}Twig:
{% for item in array %}
{{ item.id }} {{ item.title }} {{ item.var1 }} {{ item.var2 }} {{ item.var3 }} {{ item.var4 }} {{ item.var5 }} {{ item.var6 }} {{ item.var5 }} {{ item.var6 }}
{% endfor %}Result:
| Compilation | Performance | |
|---|---|---|
| Smarty 3.1.1 | 0.065 sec | 0.009 sec |
| Twig 1.2.0 | 0.131 sec | 0.082 sec |
Inheritance
Template inheritance is a very convenient mechanism. Only because of this you can fall in love with the tested template engines :) Let's find out what overhead appears when using inheritance in Smarty and Twig. To do this, create one parent template with 500 blocks, and another 500 small templates, each of which will be inherited from each other, filling out one of the 500 blocks of the parent template with static data. We give the template engine to tear the last in the chain.
Result:
| Compilation | Performance | |
|---|---|---|
| Smarty 3.1.1 | 1.329 sec | 0.002 sec |
| Twig 1.2.0 | 2.641 sec | 0.121 sec |
Total
The conclusion suggests itself: Smarty is faster than Twig. Compiling large templates takes longer, but we get better performance as a result.
For testing, a laptop was used. Pentium Dual-Core T4200 (2 GHz), 3GB RAM - nothing supernatural. The used version of PHP is 5.3. In case you have a desire to independently evaluate the performance of Smarty and Twig on your equipment, you can download the source codes of all tests in one archive .