Benchmark

A few simple benchmarks can be found in the Genshi 0.6 release. I’ve written some TDI code for bench/basic.py and bench/bigtable.py. See the chapters below for the code itself. Here are the results:

Name Basic Bigtable
Mako 0.29 ms 46.39 ms
Genshi 2.11 ms 212.89 ms
Kid 4.34 ms 858.30 ms
Clearsilver 0.17 ms 59.09 ms
Simpletal 2.18 ms (not impl)
TDI 0.06 ms 18.87 ms

Basic code

The following code was entered into basic.py:

def tdi(dirname, verbose=False):
    from tdi import html
    template = html.from_files(['base.html', 'template.html'], basedir=dirname)
    class Model(dict):
        def render_title(self, node):
            node.content = self['title']
        def render_hello1(self, node):
            node.content = 'hello %s' % self['user']
        def render_hello2(self, node):
            node.content = 'hello me'
        def render_hello3(self, node):
            node.content = 'hello world'
        def render_items(self, node):
            items = self['items']
            if not items:
                return node.remove()
            for subnode, item in node.item.iterate(items):
                subnode.content = item
            subnode['class'] = 'last'

    def render():
        return template.render_string(Model(
            title='Just a test', user='joe',
            items=['Number %d' % num for num in range(1, 15)]
        ))
    if verbose:
        print render()
    return render

The templates are:

tdi/basic.html:

<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
  <head>
    <title tdi="title"></title>
  </head>
  <body>
    <div id="header">
      <h1 tdi="title">...</h1>
    </div>

    <tdi tdi:overlay="->content" />

    <div id="footer">
    </div>
  </body>
</html>

and tdi/template.html:

<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
  <body>
    <tdi tdi:overlay="<-content">
    <p tdi="hello1">hello</p>
    <p tdi="hello2">hello</p>
    <p tdi="hello3">hello</p>

    <h2>Loop</h2>
      <ul tdi="items">
          <li tdi="item">...</li><li tdi=":-item">
          </li>
      </ul>
    </tdi>
  </body>
</html>

Bigtable code

try:
    from tdi import html as tdi
except ImportError:
    tdi = None

if tdi:
    tdi_tmpl = tdi.from_string("""
<table>
    <tr tdi="row">
        <td tdi="col"></td>
    </tr>
</table>
""")
    class tdimodel(dict):
        def render_row(self, node):
            for rownode, row in node.iterate(self['table']):
                for colnode, col in rownode.col.iterate(row.itervalues()):
                    colnode.content = col

    def test_tdi(tdimodel=tdimodel):
        """TDI template"""
        tdi_tmpl.render_string(tdimodel(table=table))

    class tdimodel_cheat(dict):
        def render_row(self, node):
            for colnode, col in \
                    node.col.iterate(self['table'][0].itervalues()):
                colnode.content = col
            for rownode, row in node.iterate(self['table']):
                pass

    def test_tdi_cheat(tdimodel=tdimodel_cheat):
        """TDI template (cheated)"""
        tdi_tmpl.render_string(tdimodel(table=table))

test_tdi and test_tdi_cheat were added to the run() function as well.

The cheating code (test_tdi_cheat) variant is incredibly fast (4.32 ms in the benchmark run above) and generates the correct HTML, but, well, it just generates the columns once and copies the whole row a thousand times. It effectively loops 1010 times instead of the 10000 times. I made it just for fun and because it’s possible, of course ;-)

The test_tdi variant however does the right thing.

Table Of Contents