Sign inorRegister Site is for SALE!

Smarty vs Twig: performance

Smarty is one of the oldest template engines for the PHP development language. If you are programming in PHP, likely you have worked with it. A third version of this template was released in 2010. Smarty 3 was written from scratch with the active use of PHP5. At the same time Smarty got an updated syntax and modern features including inheritance, sandbox (computer security) and etc.
Twig is a modern template engine from developers of the Symfony. The authors have positioned it as the fast and functional template. It looks like Smarty 3 a lot in terms of features. Twig features a slightly different syntax, as well as the stated performance. Let us verify it!


We purposely is going to use quite complex templates during testing that the processing time would be noticeable. Actually, we will evaluate this time, so we will prepare the relevant scripts.

Code for Smarty turned out very simple:

$data = json_decode(file_get_contents('data.json'), true);
$smarty = new Smarty();
$smarty->compile_check = false;
$start = microtime(true);
echo microtime(true)-$start;

Code for Twig was a bit more complicated:

$data = json_decode(file_get_contents('data.json'), true);
$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');
echo microtime(true)-$start;

Both templates were adapted in a similar way: the automatic screening was enabled and automatic recompilation of templates was disabled during changes.

Getting the values of variables

Getting the values of the variables is the most used operation. It can be used several hundred times in the complex templates. At first it may seem that the speed of this operation should not depend on a template, but it is not. There should be used some structure to store the template’s variables. The more it is simpler the faster works receipt of values of variables. We will generate a template to evaluate the performance of this operation that will produce the value of 10000 variables and nothing more.


{$var0} {$var1} {$var2} {$var3} {$var4} ...

{{ var0 }} {{ var1 }} {{ var2 }} {{ var3 }} {{ var4 }} ...
Smarty seconds0.058 seconds
Twig seconds0.083 seconds

The following table shows the average values of several consecutive tests. As we can see ten thousand syntax structures were compiled by both parties of test for a long time. Smarty is far behind Twig in this respect. However, the compilation is done only once and the compiled version of the template works further, so the running time of the last one is more important. Here Smarty is certainly faster by ≈ 30% than an opponent.

Bypass the arrays and produce values of the fields

Any serious template cannot do without foreach. Let us write a test template, which produces in 10 fields from 1000 array’s elements.


{foreach $array as $item}
{$} {$item.title} {$item.var1} {$item.var2} {$item.var3} {$item.var4} {$item.var5} {$item.var6} {$item.var5} {$item.var6}


{% for item in array %}
{{ }} {{ item.title }} {{ item.var1 }} {{ item.var2 }} {{ item.var3 }} {{ item.var4 }} {{ item.var5 }} {{ item.var6 }} {{ item.var5 }} {{ item.var6 }}
{% endfor %}

Result: .
Smarty seconds0.009 seconds
Twig seconds0.082 seconds

Here is something incredible: a compiled template Smarty runs almost in 10 times faster than version Twig! Moreover, even the compilation + execution works faster in Smarty than execution of compiled template in Twig. We can conclude that the compiler of Smarty templates is initialized faster than in Twig, but it works slower judging by the previous test that for small templates is almost unnoticeable.


Inheritance of templates is a very convenient mechanism. Only because of this can be liked the tested template engines :) Let us find out what burdens occur when we will use inheritance in Smarty and Twig. Let us create a parent template with 500 blocks and 500 of small templates that each of them will inherit from each other, and it is filling the static data in one of the 500 blocks of the parent template, and let the template engine to deal with the last in the chain.

Smarty seconds0.002 seconds
Twig seconds0.121 seconds

Smarty has worked in 60 times faster. If we look at the compiled code, it is easy to understand that this is not the limit. Smarty has united the entire chain of inherited templates into one big file, as though originally there was no inheritance. Namely, there is not performance loss as a result of the inheritance use! Twig carefully created the class and file for each template and during each generation of page uploads all that.


The conclusion is clear: Smarty is faster than Twig. A compilation of big templates takes longer, but as a result we get the better performance.
We used a laptop Pentium Dual-Core T4200 (2 GHz), 3GB RAM for testing and the version of PHP 5.3. In case, if you would like to evaluate the performance of Smarty and Twig independently on your computer, you can download the source codes for all tests in one archive.
Tags: Php, smarty, twig
xially 30 november 2011, 12:40
Vote for this post
Bring it to the Main Page


+2 utagai December 1, 2011, 7:38
As i know {{var}} syntax in twig means escaped variable so its equals to {$var | escape} in smarty. So this test is not quite fair.
0 xially December 1, 2011, 12:43
Maybe you are right. If we will use in Smarty escaped variables then the results will be something like this:
just escaped variables: Smarty 0.094, Twig 0.136
10k different escaped variables: Smarty 0.282, Twig 0.160
+1 mohrt December 1, 2011, 15:48
In the twig setup above, auto-escape is disabled, so it should be a fair test. You could try it both ways, Smarty also has an auto-escape feature $smarty->escape_html = true;
+1 RiPPeR December 16, 2011, 14:36
We did the same test early this year and came to the same conclusion.

Smarty is faster than Twig.

If you use caching system it's even more obvious.

However smarty evaluate cached template with an include() call.
If you use memcache your will need to use eval() rather than include... It's slower.
+1 1341 December 20, 2011, 15:33
I use Smarty for more than 5 years , now I use 3.1X , and I use Symfony2 , I think Twig is a very good template engine

your discuss just like to force people chose which script is better : PHP or PERL and much more like which IDE is better : KOMODO7 or ZDE9 ?

In this framework era , I often use ZDE9 and I find the feeling of pleasure that I found five years ago when I use ZDE5.51 ,
ZDE9 has the WHOLE oop support / Delicate syntax highlighting and the ULTIMATE Symfony2 / Twig / Yaml plugin , Especially the no limit SPLIT_VIEW, let me know that the Eclipse is so powerful .

but ZDE6~8 just shit .

And I still use Komodo7.0b1 and the power of Keybinding / Snippet / Regex and many other specs , is attracted me .

I use Komodo from 4 and use ZDE from 3.5 , and I abandon ZDE from version 6 till 2011-12-01 the version 9 is line up ,

but I still use ZDE9.0 because it has a incredible powerful support for Symfony2 and Twig

Twig is the choice because Symfony2 , not the performance , I think the 'extends' is a very good function , the *.html.twig file is too simple , everybody like the simple page , just like the annotation in the controllers of Symfony2 , and the yaml config system .

So I use both Smarty and Twig , and I think twig is better , though maybe twig is little slower than smarty .
( in the past many years everybody says that the smarty is slow , but they omit the power and convenience of smarty , such as the 'Componentized Templates' etc. )

If you say Twig learn from Smarty I will accept this views ,
and if you say Twig is slow so Twig is not good , you're probably wrong .
0 Carpetsmoker December 23, 2011, 15:26
Twig is a modern template engine from developers of the Symfony. The authors have positioned it as the fast and functional template. It looks like Smarty 3 a lot in terms of features.

This is not quite true, Twig is *much* more flexible and has more features, such as macros. It is also much more extensible.

It doesn't surprise me that Twig is slightly slower. But on the upside, it's a lot faster/easier to develop with.
0 Kora December 26, 2011, 20:41
Twig seems to be faster than Smarty for something, so far the faster is Rain:
+1 Tac February 1, 2012, 23:19
We used Smarty for about a year with CodeIgniter, and loved it. We're switching our framework to Symfony, and since Twig is included and tightly integrated, we're switching as well. They are both powerful template engines, so for us it came down to integration. Symfony + Twig gives a powerful debug toolbar in the development mode, which I've come to depend on.

The things I dislike about Smarty are pretty small. I hate that you have to know if it's an array or an object when de-referencing something. I wish smarty would just understand that in {$}, 'name' might be a method, might be an array index, or it might be a property. The backend code may change, I don't want to have to change the template to reflect that.

While template inheritance might be much faster, it's not as flexible. I like in Twig having the option of extending a variable template name, e.g. {% extends my_base_template %}. In Smarty, you can only have variable extends if you put the template name in the call from PHP.

But in fact I like them both. In fact, I just made the request to the Limesurvey developers that they use one instead of their internal template engine:

Which is where I'd give advise to the Smarty folks -- be proactive about integration. Smarty should be the template engine of choice with CodeIgniter, it should probably be installed with it. It's so much better than using PHP templates. Or used with Dolphin (a social networking project), Limesurvey, etc. Yes, anyone can integrate with Smarty, but if someone hadn't done it for CI, I might never have used it.

The other thing Smarty could do is some sort of better debugging. It's nice that it has the popup window, but I wish there had been more.

Last thing -- yes, Twig has a few more lines to set up, but I wish Smarty supported the autoload and had less dependencies on include/require statements. A person preference, but it feels more professional to have standardized naming and paths.
+1 Synchro February 7, 2012, 13:29
Just wanted to clear up a few things about smarty3 that are wrong in various comments.

Smarty has an spl-compatible autoloader that registers by default.

There is a debugging mode (set $smarty->debugging = true) that shows an instance-wide debug console in addition to the {debug} tag (which is only per-template), and you can enable debug mode dynamically using a SMARTY_DEBUG URL param. I find the debug output is pretty clear, giving accurate template line numbers.

You can do variable extends in the same way as twig: {extends file=$parent_file} (you may be able to do {extends $parent_file}, but I've not tried that, docs don't say).

I don't know what else I'd want a templating system to do - I can already create plugins very easily to do anything I need and since the limit here is 'whatever you can do in PHP', I fail to see how twig could be 'much more extensible' since it has pretty much identical integration hooks. I don't see macros being that interesting; they're the same thing as functions, but written in template syntax instead of PHP, and I already know PHP.

Some of my templates are very complex, using up to 6 template resources (that's resources, not subtemplates, supporting different sources, syntax and rendering intent) simultaneously (I'm not using them for web pages) and Smarty has always coped very well with that.

FWIW, so far I'm finding smarty3 slower than smarty2; might just be my odd usage pattern though.

BTW you might want to check the email validator on your registration system: it rejects some perfectly valid email addresses.
0 rodneyrehm February 8, 2012, 15:36
Twig is better than Smarty in terms of Symphony2 integration. But there are efforts to changing that fact with SmartyBundle

RiPPeR: Smarty uses eval() for cached templates, true. Smarty does this, because you can have dynamic components in a cache. A feature most template engines don't provide.

Kora: Please have a closer look at the benchmark sources provided on You will notice that the executed benchmarks are bogus. I mean, how else do you explain Dwoo taking more time for 20 iterations than for 50 (taking as much time as 10 iterations)?

Carpetsmoker, Synchro: What Twig calls macros, Smarty calls template functions.

And yes, Smarty3 is slightly slower than Smarty2. It can do a hell of a lot more, though, too. You often enough have a trade-off between feature / complexity and performance. But Smarty3 is trying hard to keep that in balance.
0 WebMas February 22, 2012, 10:24
I haven't used it yet, and I considering using RainPtl. What do you think about it? Is it better or worse then smarty?
0 drak March 26, 2012, 2:54
Would be interesting to see how this stacks up against the latest Twig (which is at 1.6.3 at the time of writing) as there have been many performance improvements.
0 DanAlexson90 October 14, 2013, 3:01
Seeing as this post is very popular when searching for Smarty vs. Twig comparisons - it desperately needs an update to account for the modern versions.
0 mad1234 December 8, 2015, 19:05
You can use also both with wee:
0 Stas July 4, 2018, 9:53
Codelobster IDE has great support for Smarty and Twig templates.

Leave a Reply

Avaible tags
  • <b>...</b>highlighting important text on the page in bold
  • <i>..</i>highlighting important text on the page in italic
  • <u>...</u>allocated with tag <u> text shownas underlined
  • <s>...</s>allocated with tag <s> text shown as strikethrough
  • <sup>...</sup>, <sub>...</sub>text in the tag <sup> appears as a superscript, <sub> - subscript
  • <blockquote>...</blockquote>For  highlight citation, use the tag <blockquote>
  • <code lang="lang">...</code>highlighting the program code (supported by bash, cpp, cs, css, xml, html, java, javascript, lisp, lua, php, perl, python, ruby, sql, scala, text)
  • <a href="http://...">...</a>link, specify the desired Internet address in the href attribute
  • <img src="http://..." alt="text" />specify the full path of image in the src attribute