Wednesday, March 12, 2008

Fastest pure python text-based templating?

Mako is a mature full-featured templating engine for python. It is also pure-python i.e. it has no C-compiled extensions, and amongst pure-python systems it is currently considered to be the fastest.

Evoque is a new lightweight templating engine for python, also pure-python and full-featured. Its simple implementation is based on the eval python builtin, and weighs in at less than 1K lines of code, compared to about 2.5K for Mako, or about 4K for Genshi. One of Evoque's distinguishing features is automatic input quoting that, to my knowledge, Mako does not do, at least not by default.

It is difficult to make sensical performance comparisons between systems -- never do such comparisons give the entire picture. I will try to highlight the specific performance results for Evoque in manual quoting mode (called evoque_mq when used this way) and those for Mako -- two systems that come close to being equivalent. The Evoque distribution contains the following three benchmarks:
  • subs - does only string substitutions
  • basic - does a variety of standard templating tasks such as looping, calling sub-templates, escaping of data
  • bigtable - a simple brute force generation of a 10 columns x 1000 rows table
These three benchmarks are run on two different platforms, as indicated below, under python 2.5.1 in each case. All times are in milliseconds (less is better).

OS X 10.3 on a 667 MHz G4 PowerBook
             subs    basic    bigtable
evoque_mq 0.27 2.26 409.71
mako 0.72 2.73 445.47
Ubuntu 7.04 on a 2.0 GHz Intel Celeron
             subs    basic    bigtable
evoque_mq 0.13 1.25 152.12
mako 0.38 1.28 142.53
Evoque is not always faster. It seems to be consistently faster for simpler templates, while Mako seems to be quicker on loop-intensive templates, such as the 1000 row table benchmark. This is probably because of additional work that Evoque has to do for the runtime evaluation of loop variables.

Labels: , , , ,

2 Comments:

Anonymous Anonymous said...

Why do you care if it is pure python?

7:54 PM  
Blogger mario said...

Pure python implies hassle-free and flexibility. No installation/compilation issues on some platforms, namely windows. No behaviour breakage, like cannot inherit from an ext-implemented class, or cannot read/modify source like rest of a python project, or just a reduced source transparency in general.

Performance is probably the most common reason for implementing specific functionality in an ext module -- at the prices mentioned above. If you get the performance and retain the malleability of pure-python... you have the best of both worlds.

11:28 AM  

Post a Comment

Links to this post:

Create a Link

<< Home