blob: 0e6b3072243a250bbd6b1f70467f765f15f424f3 [file] [log] [blame]
Armin Ronacher00d5d212008-04-13 01:10:18 +02001from django.conf import settings
2settings.configure()
3from django.template import Template as DjangoTemplate, Context as DjangoContext
4from jinja2 import Environment as JinjaEnvironment
5from mako.template import Template as MakoTemplate
6from timeit import Timer
Armin Ronacher449167d2008-04-11 17:55:05 +02007
Armin Ronacher449167d2008-04-11 17:55:05 +02008
Armin Ronacher00d5d212008-04-13 01:10:18 +02009jinja_template = JinjaEnvironment(
10 line_statement_prefix='%',
11 variable_start_string="${",
12 variable_end_string="}"
13).from_string("""\
14<!doctype html>
15<html>
16 <head>
17 <title>${page_title|e}
18 </head>
19 <body>
20 <div class="header">
21 <h1>${page_title|e}</h1>
22 </div>
23 <ul class="navigation">
24 % for href, caption in [
25 ('index.html', 'Index'),
26 ('downloads.html', 'Downloads'),
27 ('products.html', 'Products')
28 ]
29 <li><a href="${href|e}">${caption|e}</a></li>
30 % endfor
31 </ul>
32 <div class="table">
33 <table>
34 % for row in table
35 <tr>
36 % for cell in row
37 <td>${cell}</td>
38 % endfor
39 </tr>
40 % endfor
41 </table>
42 </div>
43 </body>
44</html>\
Armin Ronacher449167d2008-04-11 17:55:05 +020045""")
Armin Ronacher00d5d212008-04-13 01:10:18 +020046
47django_template = DjangoTemplate("""\
48<!doctype html>
49<html>
50 <head>
51 <title>{{ page_title }}
52 </head>
53 <body>
54 <div class="header">
55 <h1>{{ page_title }}</h1>
56 </div>
57 <ul class="navigation">
58 {% for href, caption in navigation %}
59 <li><a href="{{ href }}">{{ caption }}</a></li>
60 {% endfor %}
61 </ul>
62 <div class="table">
63 <table>
64 {% for row in table %}
65 <tr>
66 {% for cell in row %}
67 <td>{{ cell }}</td>
68 {% endfor %}
69 </tr>
70 {% endfor %}
71 </table>
72 </div>
73 </body>
74</html>\
75""")
76
77mako_template = MakoTemplate("""\
78<!doctype html>
79<html>
80 <head>
81 <title>${page_title|h}
82 </head>
83 <body>
84 <div class="header">
85 <h1>${page_title|h}</h1>
86 </div>
87 <ul class="navigation">
88 % for href, caption in [('index.html', 'Index'), ('downloads.html', 'Downloads'), ('products.html', 'Products')]:
89 <li><a href="${href|h}">${caption|h}</a></li>
90 % endfor
91 </ul>
92 <div class="table">
93 <table>
94 % for row in table:
95 <tr>
96 % for cell in row:
97 <td>${cell}</td>
98 % endfor
99 </tr>
100 % endfor
101 </table>
102 </div>
103 </body>
104</html>\
105""")
106
107context = {
108 'page_title': 'mitsuhiko\'s benchmark',
109 'table': [dict(a=1,b=2,c=3,d=4,e=5,f=6,g=7,h=8,i=9,j=10) for x in range(1000)]
110}
111
112def test_jinja():
113 jinja_template.render(context)
114
115def test_django():
116 c = DjangoContext(context)
117 c['navigation'] = [('index.html', 'Index'), ('downloads.html', 'Downloads'), ('products.html', 'Products')]
118 django_template.render(c)
119
120def test_mako():
121 mako_template.render(**context)
122
123
124for test in 'jinja', 'mako', 'django':
125 t = Timer(setup='from __main__ import test_%s as bench' % test,
126 stmt='bench()')
127 print '%-20s%.4fms' % (test, t.timeit(number=20) / 20)