Code Irony

An Ironic Look at the State of the Art by Christopher Myers and Justin Davis

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:

graph
0.029%! Are you kidding?!? Sure, other processes take time too, but those problems look hard to solve. The obvious bottleneck that we can fix is the template rendering. The kludgy template system just has to go! We’ve decided to solve this by implementing our own template system. Since we’ve actually taken the time to think about it (unlike those other templating solutions), we’ll name it ConTemplate. It works like this: Say you want to create a basic web page, and have it show your name at the top. First create a template file that looks like this:

<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:
myshark

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

  1. thomas lackner February 7th, 2008 9:10 pm

    looping.

  2. brian February 7th, 2008 9:19 pm

    LOL love the sarcasm

  3. Jonathan Mark February 7th, 2008 9:48 pm

    You meant 29%

  4. percent February 8th, 2008 1:21 am

    29%?

    0.02727273, so he’s right, no?

  5. Robins February 8th, 2008 1:22 am

    I think as per the graph… he did mean .029% (which is 0.00029) !

  6. percent February 8th, 2008 1:23 am

    Oh, 29% was referring to another number. I think he’s being sarcastic there.

  7. anon February 8th, 2008 1:31 am

    no actually he means 3. idiot.

  8. anon February 8th, 2008 4:33 am

    that comparison is stupid, fix in some security, any number of variables, content driven caching and try again..

  9. Armin Ronacher February 8th, 2008 7:47 am

    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.

  10. Dezro February 8th, 2008 8:16 am

    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?

  11. Harald Armin Massa February 8th, 2008 9:33 am

    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.

  12. Mike Cantelon February 8th, 2008 11:14 am

    Epic win, gents. I’m hereby reinventing my business as a presentation layer optimization consultancy with ConTemplate as my Excalibur.

  13. rektide February 8th, 2008 4:44 pm

    your solution really takes 100ms to just do templating?

  14. Michael Carter February 8th, 2008 6:17 pm

    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?

  15. Bill G February 9th, 2008 12:06 am

    I use .NET, will this template be available for .NET?

    Thanks!

  16. Eric Florenzano February 9th, 2008 2:12 am

    Lol! I love the serious responses you’re getting–classic!

  17. Handsome Greg February 13th, 2008 6:18 pm

    Hilarious. Who _are_ these people who don’t get the joke?

  18. Terry Nova February 15th, 2008 12:23 pm

    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

  19. Ricardo Montalban February 22nd, 2008 9:59 pm

    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!

  20. gavin March 4th, 2008 12:29 am

    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.

  21. Julianaoe March 24th, 2008 9:26 am

    Nice post., guy

  22. Dude August 24th, 2008 10:41 pm

    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.

Leave a reply