Fastest templating engine ever. Period.
Since web frameworks seem to be the only thing people are doing in Python these days, templating languages have become increasingly important. The problem that we at Code Irony keep running into is that they’re just not fast enough. How slow are they? We ran some profiling across one of our projects and this is what we’ve found:
<h1>Hello %(name)s</h1>
After many usability tests, we found that the %(variable)s syntax is the easiest for people to understand. Furthermore, we want to be sure to separate the logic from the presentation, so we only want to replace strings. Save this file to hello.html Then, in your web framework, just import our template engine and use the code like this:

from contemplate import ConTemplate
mytemplate = ConTemplate("hello.html")
name = "Guido"
print mytemplate.render()
> <h1>Hello Guido</h1>
Look! ConTemplate filled in your name and everything! Now, we all know that speed is the real factor in making a templating language. Check out how ConTemplate stacks up against some of the competition:
| Template | Processing Time |
|---|---|
| Genshi | .110s |
| Mako | .109s |
| ConTemplate | .107s |
Wow! On our first try, we beat the two fastest python templating languages! And Genshi lost by almost 3%! Imagine how fast it’ll be when version 2 comes out! You can get the benchmark test here and download the implementation here, or browse it below.
# Filename: contemplate.py # Revision: 0.1 # # Copyright 2008 Christopher Myers and Justin Davis # # “ConTemplate” # This is by far the fastest python templating language you’ve ever used. # # You have unlimited license to use this software however you want. There # is no warranty either expressed nor implied. Actually, we recommend not # using it. It just exists to prove a point. import inspect class ConTemplate(object): def __init__(self, filename): “”“Load the template”“” try: self.template = open(filename).read() except IOError: raise IOError, “No file at that location” def render(self, locs = None): “”“Render the template”“” if not locs: locs = globals() locs.update(inspect.currentframe(1).f_locals) return self.template % locs
22 Comments so far
Leave a reply
looping.
LOL love the sarcasm
You meant 29%
29%?
0.02727273, so he’s right, no?
I think as per the graph… he did mean .029% (which is 0.00029) !
Oh, 29% was referring to another number. I think he’s being sarcastic there.
no actually he means 3. idiot.
that comparison is stupid, fix in some security, any number of variables, content driven caching and try again..
That implementation is broken. globals() returns the global namespace, not a reference to this. As a result of that you are updating the module where the template is located in with the rendering locals. Neither nice nor threadsafe.
I tried to beat you with a little secret weapon I like to call string.Template, but it didn’t quite work out…
ConTemplate: 0.0673758983612
ProTemplate: 0.0676801204681
Still! Pretty close, aren’t I?
this library was presented @ europython 2006 @ CERN, Switzerland. It’s name is zerolib and it is the final templating solution for Python, no installs needed, since the name zerolib.
Epic win, gents. I’m hereby reinventing my business as a presentation layer optimization consultancy with ConTemplate as my Excalibur.
your solution really takes 100ms to just do templating?
This solution is just dumb. Since you can only do string replacement, all of your looping and conditional logic will happen in your code. You will be concatenating strings together inside python for loops — thats the worst idea you’ve had today.
Did you try mako or cheetah for python?
I use .NET, will this template be available for .NET?
Thanks!
Lol! I love the serious responses you’re getting–classic!
Hilarious. Who _are_ these people who don’t get the joke?
LOVE your blog, thanks for entertaining me
Hope there will be more posts soon
regards, terry
ps - sorry im not that good in writing in english because I came from europe - but i understand a lot
I bet things would be a lot faster if you didn’t use a template system and just used static coded webpages. Who cares about maintainability? It’s all about the speed, baby!
I think the point of this blog is …
the speed of a template engine is absolutely meaningless. The ease of use/maintainability of a template engine is the only thing that matters.
Nice post., guy
Excelent. Your blog is really interesting. To have a good blog you should not only to post smth, but do it with your soul. You do your best.