Skip to content
Myr

bench.py

This file

Path

examples/bench.py

127 lines · 2.8 KiB · Python

Last changed by

moved example tests into a folderArmin Ronacherbefore adoption2e7b54f

History of this file

Take it

1from django.conf import settings2settings.configure()3from django.template import Template as DjangoTemplate, Context as DjangoContext4from jinja2 import Environment as JinjaEnvironment5from mako.template import Template as MakoTemplate6from timeit import Timer789jinja_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    % endfor31    </ul>32    <div class="table">33      <table>34      % for row in table35        <tr>36        % for cell in row37          <td>${cell}</td>38        % endfor39        </tr>40      % endfor41      </table>42    </div>43  </body>44</html>\45""")4647django_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""")7677mako_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    % endfor91    </ul>92    <div class="table">93      <table>94      % for row in table:95        <tr>96        % for cell in row:97          <td>${cell}</td>98        % endfor99        </tr>100      % endfor101      </table>102    </div>103  </body>104</html>\105""")106107context = {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}111112def test_jinja():113    jinja_template.render(context)114115def test_django():116    c = DjangoContext(context)117    c['navigation'] = [('index.html', 'Index'), ('downloads.html', 'Downloads'), ('products.html', 'Products')]118    django_template.render(c)119120def test_mako():121    mako_template.render(**context)122123124for 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)