Commits

zzzeek  committed 72a2071

speed tests, jinja2 vs. mako, using two different mako approaches

  • Participants

Comments (0)

Files changed (7)

File jinja2/hello.jinja2

+{% extends "master.jinja2" %}
+{% block content %}
+    <p>Lorem ipsum dolor sit amet, consecteteur adipiscing elit nisi ultricies. Condimentum vel, at augue nibh sed. Diam praesent metus ut eros, sem penatibus. Pellentesque. Fusce odio posuere litora non integer habitant proin. Metus accumsan nibh facilisis nostra lobortis cum diam tellus. Malesuada nostra a volutpat pede primis congue nisl feugiat in fermentum. Orci in hymenaeos. Eni tempus mi mollis lacinia orci interdum lacus. Sollicitudin aliquet, etiam. Ac. Mi, nullam ligula, tristique penatibus nisi eros nisl pede pharetra congue, aptent nulla, rhoncus tellus morbi, ornare. Magna condimentum erat turpis. Fusce arcu ve suscipit nisi phasellus rutrum a dictumst leo, laoreet dui, ultricies platea. Porta venenatis fringilla vestibulum arcu etiam condimentum non.</p>
+    
+    <table border="1">
+        {% for k, v in environment.iteritems() %}
+            <tr><td>{{ k | escape }}</td><td>{{ v | escape }}</td></tr>
+        {% endfor %}
+    </table>
+{% endblock %}

File jinja2/master.jinja2

+<!DOCTYPE html>
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
+<head>
+    <title>Hello World</title>
+</head>
+<body>
+    {% block content %}{% endblock %}
+</body>
+</html>

File mako/hello.mako

+<%inherit file="master.mako"/>
+    <p>Lorem ipsum dolor sit amet, consecteteur adipiscing elit nisi ultricies. Condimentum vel, at augue nibh sed. Diam praesent metus ut eros, sem penatibus. Pellentesque. Fusce odio posuere litora non integer habitant proin. Metus accumsan nibh facilisis nostra lobortis cum diam tellus. Malesuada nostra a volutpat pede primis congue nisl feugiat in fermentum. Orci in hymenaeos. Eni tempus mi mollis lacinia orci interdum lacus. Sollicitudin aliquet, etiam. Ac. Mi, nullam ligula, tristique penatibus nisi eros nisl pede pharetra congue, aptent nulla, rhoncus tellus morbi, ornare. Magna condimentum erat turpis. Fusce arcu ve suscipit nisi phasellus rutrum a dictumst leo, laoreet dui, ultricies platea. Porta venenatis fringilla vestibulum arcu etiam condimentum non.</p>
+    
+    <table border="1">
+        
+        % for k, v in environment.iteritems():
+            <tr><td>${ k | h }</td><td>${ v | h }</td></tr>
+        
+        % endfor
+    </table>

File mako/master.mako

+<!DOCTYPE html>
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
+<head>
+    <title>Hello World</title>
+</head>
+<body>
+    ${next.body()}
+</body>
+</html>

File mako_def/hello.mako

+<%inherit file="master.mako"/>
+<%def name="content()">
+    <p>Lorem ipsum dolor sit amet, consecteteur adipiscing elit nisi ultricies. Condimentum vel, at augue nibh sed. Diam praesent metus ut eros, sem penatibus. Pellentesque. Fusce odio posuere litora non integer habitant proin. Metus accumsan nibh facilisis nostra lobortis cum diam tellus. Malesuada nostra a volutpat pede primis congue nisl feugiat in fermentum. Orci in hymenaeos. Eni tempus mi mollis lacinia orci interdum lacus. Sollicitudin aliquet, etiam. Ac. Mi, nullam ligula, tristique penatibus nisi eros nisl pede pharetra congue, aptent nulla, rhoncus tellus morbi, ornare. Magna condimentum erat turpis. Fusce arcu ve suscipit nisi phasellus rutrum a dictumst leo, laoreet dui, ultricies platea. Porta venenatis fringilla vestibulum arcu etiam condimentum non.</p>
+    
+    <table border="1">
+        
+        % for k, v in environment.iteritems():
+            <tr><td>${ k | h }</td><td>${ v | h }</td></tr>
+        
+        % endfor
+    </table>
+</%def>

File mako_def/master.mako

+<!DOCTYPE html>
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
+<head>
+    <title>Hello World</title>
+</head>
+<body>
+    ${self.content()}
+</body>
+</html>
+from jinja2 import Environment, FileSystemLoader, __version__ as jinja_version
+jinja_env = Environment(loader=FileSystemLoader('./jinja2'))
+
+from mako.lookup import TemplateLookup
+from mako import __version__ as mako_version
+
+mako_env = TemplateLookup('./mako')
+
+mako_def_env = TemplateLookup('./mako_def')
+
+import timeit
+
+environment = dict(("key_%d" % i, "value_%d" % i) for i in xrange(50))
+
+def jinja2():
+    template = jinja_env.get_template('hello.jinja2')
+    val = template.render(environment=environment)
+    return val
+    
+def mako():
+    template = mako_env.get_template("hello.mako")
+    val = template.render(environment=environment)
+    return val
+
+def mako_def():
+    template = mako_def_env.get_template("hello.mako")
+    val = template.render(environment=environment)
+    return val
+
+# content is identical
+assert jinja2() == mako()
+assert jinja2() == mako_def() 
+
+print "jinja2 %s:" % jinja_version, timeit.timeit(
+                                        "jinja2()", 
+                                        "from __main__ import jinja2", 
+                                        number=10000)
+                                        
+print "mako %s:" % mako_version, timeit.timeit(
+                                        "mako()", 
+                                        "from __main__ import mako", 
+                                        number=10000)
+
+print "mako %s using def:" % mako_version, timeit.timeit(
+                                        "mako_def()", 
+                                        "from __main__ import mako_def", 
+                                        number=10000)
+
+
+