Benchmarks
==========

These benchmarks should not be taken too seriously but they do give an
idea of how this package measures up to Zope's default TAL
engine. Also included is a comparison to a pure python implementation.

Results
-------

                  zope.pagetemplate     z3c.pt    pure python
Hello World        3.6                  1         0.02        
1000 x 10 table   14.0                  1         0.63

There's a setup cost in using a template language which explains the
50x factor in the 'Hello World' benchmark versus a pure python
implementation.

Certainly a specialized implementation will always be faster. But
there's still some room for improvement.

Benchmark source code
---------------------

  >>> from z3c.pt import PageTemplate
  >>> from zope.pagetemplate.pagetemplate import PageTemplate as z3PageTemplate

Hello World:

  >>> template = PageTemplate("""\
  ... <div xmlns="http://www.w3.org/1999/xhtml">
  ...   Hello World!
  ... </div>""")

  >>> # for i in range(90000): a = template()

  >>> template = z3PageTemplate()
  >>> template.pt_edit("""\
  ... <div xmlns="http://www.w3.org/1999/xhtml">
  ...   Hello World!
  ... </div>""", 'text/xhtml')

  >>> # for i in range(90000): a = template()  

  >>> def hello_world():
  ...     return u"""\
  ... <div>
  ...   Hello World!
  ... </div>"""

  >>> # for i in range(9000000): a = hello_world()

1000 x 10 table:
  
  >>> 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)]

  >>> template = PageTemplate("""\
  ... <table xmlns="http://www.w3.org/1999/xhtml"
  ...        xmlns:tal="http://xml.zope.org/namespaces/tal">
  ...   <tr tal:repeat="row table">
  ...      <td tal:repeat="c row.values()">
  ...          <span tal:define="d c + 1"
  ...                tal:attributes="class 'column-' + str(d)"
  ...                tal:content="d" />
  ...      </td>
  ...   </tr>
  ... </table>""")

  >>> for i in range(40): a = template(table=table)
  
  >>> template = z3PageTemplate()
  >>> template.pt_edit("""\
  ... <table xmlns="http://www.w3.org/1999/xhtml"
  ...        xmlns:tal="http://xml.zope.org/namespaces/tal">
  ...   <tr tal:repeat="row options/table">
  ...      <td tal:repeat="c python: row.values()">
  ...          <span tal:define="d python: c + 1"
  ...                tal:attributes="class string:column-${d}"
  ...                tal:content="d" />
  ...      </td>
  ...   </tr>
  ... </table>""", 'text/xhtml')

  >>> # for i in range(40): a = template(table=table)

  >>> from StringIO import StringIO
  >>> def bigtable(table):
  ...   out = StringIO()
  ...   for row in table:
  ...      out.write('<tr>')
  ...      for c in row.values():
  ...         d = c+1
  ...         out.write('<td><span class="column-%d">%s</span></td>' % (d, d))
  ...      out.write('</tr>')
  ...   return out.getvalue()
  
  >>> # for i in range(40): a = bigtable(table=table)

