Skip to content
Alexey Rybak edited this page Feb 19, 2015 · 2 revisions

Benchmarks

Unfortunately, I don't know any simple, universal and really correct method to analyse template engine performance. The only one right way to do this is to build your application using several template engines and measure the performance under the real conditions. Should it be noted that nobody is going to do this? That's why we always deal with artificial tests which results should be used very carefully. Nevertheless, results of one "synthetic" test are listed here.

This test is much closer to real world conditions than most of tests you can find in the Internet (like "wow, we have printed this variable 200 times!!!11"). First we have a page of several non-trivial parts. The code is written using different template engines. This page is built by a webserver, and the performance is benchmarked using the standard apache ab utility. The page simulates a some pseudo portal main page and contains:

  • adverts (3 items)
  • news list (5 items, 5 vars each)
  • striped navigation (7 sections)
  • list of users online(~20 items)
  • vote with 3 possible answers
  • other variables (~10 items)

Tests include lot of different template engines. Leaders are:

  • PHP mess: PHP and HTML are messed in a single file. This method is used in the real big projects in very specific cases only, but included into the tests just because it is obviously the fastest one.
  • blitz: single template, every functional block has it's own context
  • PHP includes: PHP and HTML are messed but functionally different blocks(list elements) are separated and included from the main code.
  • php_templates: single template, every functional block has it's own context
  • smarty: single template, compiled, output cache is off

The results of this test can be found below.

Benchmark results

Software versions and settings were:

PHP 4.3.10, ZPS 4.0.2
Sigma 1.1.5 (cache on)
Smarty 2.6.15 (tpl-compile on, output-cache off)
Blitz 0.4.3
FastTemplate 1.1.0
XTemplate 0.3.0
php_templates 1.7
cTemplate 0.8 (ctemplate 0.4, nothreads)

These measurements were made quite long time ago (PHP 4.3.* with Zend Performance Suite, and old versions of template engines). Now we need to make the same test with modern PHP 5.3.* with APC. If you can help with this - please drop me an email. I still need benchmark tests for some popular systems like CTPP, ClearSilver, Twig, Lapa, Quicky, Macro/WACT or possibly many others. If you just send me how to implement the test with these systems - I'll test it and add the results here. The benchmark code is available to download and one can add benchmark code for any other template engine.

Results above should be interpreted in a very generalized way. Native PHP-code is obviuosly the fastest. What is more important, "native" here means written by hands, not "compiled". If you ever looked into a "compiled" template you probably know that its code has a lot of nested constructions, quite hard to execute (with a lot of unnecessary hash lookups, for instance), much more complex than written by right hands code ;) One more important thing is obvious as well: the less files you have the most effective your code is, and Blitz context methods are faster than include. I used single file methods for both php_templates and Smarty to squeeze maximum from them, so i suppose Blitz to be visibly faster.

The difference between Blitz and commonly used "php includes" method is not very big. In real world application any difference will be likely hidden against the background of other code especially database requests et cetera. Thus both of the methods could be considered as practically equal. You can notice that most of the tests were made using an accelerator from ZPS. Using accelerator is very significant for PHP code - but you possibly can obtain different results using other products like APC/XCache/eAccelerator/whatever. In general, one should not rely on the quoted results entirely. Make tests, play with real projects and choose solutions, which give a profit under your own particular conditions.

Slowdowns

Blitz performance recipes are the same as for any web-applications with CPU bottleneck. Following things in Blitz may slow down your application:

  • lot of includes (all the data will seat in VFS cache almost immediately, but includes always add some kernel syscalls and need CPU to parse/initialise objects).
  • lot of hash lookups to resolve path-variables $obj.smth.blabla, performance downgrades proportionally to the product number_of_variables*average_path_length
  • lot of hash lookups back through the scope stack
  • lot of block()/iterate() calls instead of preparing set data and just calling $View->display($data)
  • lot of non-built-in Blitz methods in templates (user defined callbacks or PHP-functions)