= Performance of the Genshi Template Engine = The directory [source:/trunk/examples/bench/ examples/bench] has some very simple benchmarks. ''It should be noted that text-based template engines generally have a '''lot''' less work to do than XML-based ones, simply because their functionality is limited to processing text as opposed to processing markup.'' == `basic` benchmark == The [source:/trunk/examples/bench/basic.py basic.py] benchmark is based on a relatively simple template that includes a lot of the common templating constructs, such as includes/inheritance, looping, conditionals, macros, etc. All tests run under on Ubuntu 12.10 (Intel(R) Core(TM) i5-3210M CPU @ 2.50GHz). || '''Template Engine''' || '''Python version''' || '''Rendering time (less is better)''' || '''Notes''' || |||||||||| || Genshi || CPython 2.7 || 1.06 ms || Genshi markup template || || Genshi_text || CPython 2.7 || 0.36 ms || The text-based template engine included with Genshi || |||||||||| || Genshi || CPython 3.3 || 1.41 ms || Genshi markup template || || Genshi_text || CPython 3.3 || 0.49 ms || The text-based template engine included with Genshi || |||||||||| || Genshi || PyPy 2.7 || 2.42 ms || Genshi markup template || || Genshi_text || PyPy 2.7 || 0.50 ms || The text-based template engine included with Genshi || {{{ #!comment || [http://kid-templating.org/ Kid] || 6.16 ms || Kid templates are compiled to Python byte-code. The cElementTree implementation was used for this test, although using the Python implementation doesn't appear to make a significant difference. || || [http://www.djangoproject.com/documentation/templates/ Django] || 1.38 ms || Text-based template engine that comes with the [http://www.djangoproject.com/ Django] framework. || || [http://www.clearsilver.net/ Clearsilver] || 0.30 ms || !ClearSilver is a text-based template engine written in C. || || [http://www.makotemplates.org/ Mako] || 0.31 ms || Text-based template engine written in Python || || [http://www.owlfish.com/software/simpleTAL/ SimpleTAL] || 2.84 ms || Another XML-based template engine written in Python, based on the [http://www.zope.org/ Zope] template engine (TAL). || }}} The test template is rather simple right now and doesn't make much use of the advanced features provided by either Genshi, because then it would be impossible to implement an equivalent template using other template languages. == `bigtable` benchmark == The [source:/trunk/examples/bench/bigtable.py bigtable.py] benchmark renders a large HTML table (with 1000 rows and 10 columns) in two nested loops, where each cell contains a variable reference. This basically measures the raw throughput of the template engine, while not using any advanced features. || '''Template Engine''' || '''Python version''' || '''Rendering time (less is better)''' || '''Notes''' || |||||||||| || Genshi tag builder || CPython 2.7 || 192 ms || || || Genshi template || CPython 2.7 || 107 ms || || || Genshi text template || CPython 2.7 || 74 ms || || || Genshi template + tag builder || CPython 2.7 || 212 ms || || |||||||||| || Genshi tag builder || CPython 3.3 || 266 ms || || || Genshi template || CPython 3.3 || 134 ms || || || Genshi text template || CPython 3.3 || 95 ms || || || Genshi template + tag builder || CPython 3.3 || 287 ms || || |||||||||| || Genshi tag builder || !PyPy 2.7 || 103 ms || || || Genshi template || !PyPy 2.7 || 74 ms || || || Genshi text template || !PyPy 2.7 || 43 ms || || || Genshi template + tag builder || !PyPy 2.7 || 106 ms || || {{{ #!comment || [http://kid-templating.org/ Kid] || 1101 ms || || || [http://www.djangoproject.com/documentation/templates/ Django] || 474 ms || || || [http://www.makotemplates.org/ Mako] || 96 ms || || || [http://effbot.org/zone/element-index.htm ElementTree] || 293 ms || ''Not a template engine'': the benchmark simply constructs the XML tree in memory and serializes it. || || [http://effbot.org/zone/celementtree.htm cElementTree] || 179 ms || ''Not a template engine'': the benchmark simply constructs the XML tree in memory and serializes it. || || [http://www.clearsilver.net/ Clearsilver] || 83 ms || || ''Note that both ET and cET are using Python code for serialization.'' }}} == Ideas for improving the performance == See Christopher Lenz's [http://groups.google.com/group/trac-dev/msg/fe3bfd51a880a3e7 mail to Trac-dev] for a more detailed discussion. Ideas for performance improvements: * Dropping support for dynamic XInclude tags, i.e. Genshi would need to know what gets included at parse time. * Moving match template processing to the parsing stage (aka static match templates); or alternatively, drop the match template idea altogether and move to the more conventional model of template inheritance where the master predefines slots that can be filled. * Compiling templates to Python bytecode. * Providing a fragment caching system. * Etc. Many of these represent substantial changes an would effectively result in Genshi2. Currently Genshi is roughly twice as fast on PyPy as on CPython 2.7. == See Also == If you need only a subset of Genshi, you may want to have a look at the [http://code.google.com/p/genshi-compiler/ genshi-compiler] project, which claims a 40x times speedup, at the price of leaving out `py:match`, full i18n support and more ... (so basically it's not usable by Trac for instance).