Python templating comparison by memory consumption
Another comparison between:
Results:
- standard formatting;
- more advanced standard string.template;
- Mako
- Genshi
- Jinja2
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:
Method | RSS (kB) |
---|---|
standard formatting | 4988 |
string.template | 5020 |
Jinja2 | 7188 |
Genshi | 7388 |
Mako | 9148 |
Comments
Post a Comment