Python templating comparison by memory consumption

Another comparison between:

Here the code I used for measuring:

#!/usr/bin/env python

import sys

NAME = 'name'


def render1():
    template = "<p>Hello %s!</p>"
    return template % NAME


def render2():
    from string import Template
    template = Template("<p>Hello ${name}!</p>")
    return template.substitute(dict(name=NAME))


def render3():
    from mako.template import Template
    template = Template("<p>Hello ${name}!</p>")
    return template.render(name=NAME)


def render4():
    from genshi.template import MarkupTemplate
    tmpl = MarkupTemplate('<p>Hello $name!</p>')
    stream = tmpl.generate(name=NAME)
    return stream.render('xhtml')


def render5():
    from jinja2 import Template
    template = Template('<p>Hello {{ name }}!</p>')
    return template.render(name=NAME)


if __name__ == '__main__':
    num = int(sys.argv[1])
    print globals()['render%d' % num]()
    raw_input('Press Enter...')

Results:

MethodRSS (kB)
standard formatting4988
string.template5020
Jinja27188
Genshi7388
Mako9148

Comments

Popular posts from this blog

DIY: Business cards in LaTeX

Python vs JS vs PHP for embedded systems

Shellcode detection using libemu