Smarty is the most classic template engine in the PHP language, and if you've ever developed a PHP program, you've used it more or less. Smarty released its third edition in 2010, Smarty 3 was reconstructed with the latest PHP5. It retains the original syntax and adds some more modern features. Twig is a developer from Symfony, twig the author to position it as a fast and powerful modern template engine. Twig has many features similar to Smarty 3, but slightly different to improve performance. Now let's do a performance comparison test for both:
Test we have prepared a more complex template so that we can clearly see the execution time of the program, the following is our code: The same function, looks like the Smarty implementation is simpler:
$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 ');
Twig more complicated:
$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);
Both programs have the same configuration: Turn off two compilations, do not display pages, leaving only the display of execution time. Getting a value from a variable getting a value from a variable is a more common operation and may be used hundreds of times in a more complex template development. We might think that the execution speed of the operation does not need to depend on the template, but is not, the template engine will use some data structures in the template to store the value of the variable, so the operation to get the value of the variable requires simpler and faster performance. Below we will generate a 10,000-value template to compare performance. Smarty:
{$var 0} {$var 1} {$var 2} {$var 3} {$var 4} ... |
Twig:
{{var0}}} {{var1}}} {{VAR2}}} {{VAR3}}} {{VAR4}} ... |
Result:
|
Compiling |
Execution |
Smarty 3.1.1 |
16.320 seconds |
0.058 seconds |
Twig 1.2.0 |
9.757 seconds |
0.083 seconds |
The table above shows the average of multiple consecutive Tests. We can see that the program has been compiled to generate 10,000 variables of the template, Smarty at compile time is far behind the twig. However, the compilation is only the first time to execute, and then will always use the compiled page, so the compiled page execution speed is more important. After compiling the execution time, Smarty is about 30% faster than the twig, using multiple foreach to test the common development template often used foreach, where we used 1000 with 10 elements of the array, to test the two template engine foreach performance.
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}
Twig:
{% for item in array%}
{{Item.id}} {{Item.title}} {{item.var1}} {{Item.var2}} {{Item.var3}} {{ITEM.VAR4}}} {{ITEM.VAR5}} { 6} {{ITEM.VAR5}}} {{ITEM.VAR6}}}
Result:.