Back to Home

Smarty vs. Twig: performance

php · smarty · twig

Smarty vs. Twig: performance

    Smarty is one of the oldest template engines for PHP. If you program in PHP - most likely, you had to work with it. In 2010, the third version of this template engine was released. Smarty 3 was written from scratch, with active use of PHP5. Along with this, Smarty received updated syntax and modern features, including inheritance , sandbox , etc.
    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:
    CompilationPerformance
    Smarty 3.1.116.320 sec.0.058 seconds
    Twig 1.2.09.757 sec0.083 sec
    The table shows the average values ​​from several consecutive tests. As you can see, ten thousand syntactic constructs compiled for a very long time by both participants in the test. Smarty in this regard is far behind Twig. However, compilation is performed only once, and in the future, the already compiled version of the template works, so the run time of the latter is much more important. And here Smarty is confidently ≈30% faster than the opponent.

    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:
    CompilationPerformance
    Smarty 3.1.10.065 sec0.009 sec
    Twig 1.2.00.131 sec0.082 sec
    Something incredible is happening here: the compiled Smarty template runs almost 10 times faster than the Twig option! Moreover, even compilation + execution in Smarty is faster than executing a ready-made compiled template in Twig. From this we can conclude that the Smarty template compiler initializes faster than Twig, but judging by the previous test it works more slowly, which is almost imperceptible on small templates.

    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:
    CompilationPerformance
    Smarty 3.1.11.329 sec0.002 sec
    Twig 1.2.02.641 sec0.121 sec
    Smarty worked 60 times faster. If you look at the compiled code, you can easily see that this is not the limit. Smarty combined the entire chain of inherited templates into one large file, as if there was no inheritance initially. That is, as a result, when using inheritance, there are no performance losses at all! Twig has carefully created for each template a beautiful class and file, and with each page generation it loads all this stuff.

    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 .

    Read Next