What is the fastest template system for Python?
Here are the results of the popular template engines for rendering a 10x1000 HTML table.
Python 2.6.2 on a 3GHz Intel Core 2Kid template 696.89 msKid template + cElementTree 649.88 msGenshi template + tag builder 431.01 msGenshi tag builder 389.39 msDjango template 352.68 msGenshi template 266.35 msElementTree 180.06 mscElementTree 107.85 msStringIO 41.48 msJinja 2 36.38 msCheetah template 34.66 msMako Template 29.06 msSpitfire template 21.80 msTenjin 18.39 msSpitfire template -O1 11.86 mscStringIO 5.80 msSpitfire template -O3 4.91 msSpitfire template -O2 4.82 msgenerator concat 4.06 mslist concat 3.99 msgenerator concat optimized 2.84 mslist concat optimized 2.62 ms
The benchmark is based on code from Spitfire performance tests with some added template engines and added iterations to increase accuracy. The list and generator concat at the end are hand coded Python to get a feel for the upper limit of performance achievable by compiling to Python bytecode. The optimized versions use string interpolation in the inner loop.
But before you run out to switch your template engine, make sure it matters. You'll need to be doing some pretty heavy caching and really optimized code before the differences between the compiling template engines starts to matter. For most applications good abstraction facilities, compatibility with design tools, familiarity and other things matter much much more.
From the jinja2 docs, it seems that string.Template is the fastest if that's all you need.
Without a doubt you should try to remove as much logic from templates as possible. But templates without any logic mean that you have to do all the processing in the code which is boring and stupid. A template engine that does that is shipped with Python and called string.Template. Comes without loops and if conditions and is by far the fastest template engine you can get for Python.
If you can throw caching in the mix (like memcached) then choose based on features and ease of use rather than optimization.
I use Mako because I like the syntax and features. Fortunately it is one of the fastest as well.