Anonymous avatar Anonymous committed 72a2071

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

Comments (0)

Files changed (7)

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 %}

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

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>

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)
+
+
+
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.