views:

2547

answers:

9

Jinja2 and Mako are both apparently pretty fast.

How do these compare to (the less featured but probably good enough for what I'm doing) string.Template ?

+1  A: 

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.

Josh Gibson
A: 

http://teng.sourceforge.net/ - designed for 100+req/sec sites.

+1  A: 

In general you will have to do profiling to answer that question, as it depends on how you use the templates and what for.

string.Template is the fastest, but so primitive it can hardly be called a template in the same breath as the other templating systems, as it only does string replacements, and has no conditions or loops, making it pretty useless in practice.

Lennart Regebro
A: 

According to the benchmarks offered on the website, Tenjin is supposedly blazingly fast compared to the competition.

Mike Trpcic
hmm... the handling of indentation in this looks extremely uncomfortable, however...
bobince
According to the benchmarks on their websites, every template engine is blazingly fast compared to the competition.
Iceman
+9  A: 

You probably don't care how fast the templating system is. Among the popular ones, they all have perfectly acceptable performance characteristics. Please make decisions like this based on more important things, like ease of programming.

Christian Oudard
http://www.codeirony.com/?p=9
insin
A: 

I think Cheetah might be the fastest, as it's implemented in C.

Koen Bok
Just because something is written in C, it does not mean that it will be faster; These things highly depend on the developer.
kzh
+15  A: 

Here are the results of the popular template engines for rendering a 10x1000 HTML table.

Python 2.6.2 on a 3GHz Intel Core 2

Kid template                         696.89 ms
Kid template + cElementTree          649.88 ms
Genshi template + tag builder        431.01 ms
Genshi tag builder                   389.39 ms
Django template                      352.68 ms
Genshi template                      266.35 ms
ElementTree                          180.06 ms
cElementTree                         107.85 ms
StringIO                              41.48 ms
Jinja 2                               36.38 ms
Cheetah template                      34.66 ms
Mako Template                         29.06 ms
Spitfire template                     21.80 ms
Tenjin                                18.39 ms
Spitfire template -O1                 11.86 ms
cStringIO                              5.80 ms
Spitfire template -O3                  4.91 ms
Spitfire template -O2                  4.82 ms
generator concat                       4.06 ms
list concat                            3.99 ms
generator concat optimized             2.84 ms
list 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.

Ants Aasma
I didn't know that Django template is that sloow.
jpartogi
@Ants - I didn't either. It's a small part of the equation for most, but if you're rendering a 10x1000 table of data, you're in trouble.
orokusaki
+1  A: 

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.

Tony