[Twisted-web] [Nevow] template performance
Manlio Perillo
manlio_perillo at libero.it
Fri Dec 15 06:07:49 CST 2006
Alvin Wang ha scritto:
> Just curious. How much slower?
>
10 times!
manlio at synapsis:~/projects/tests/django$ python static.py
33.58 usec/pass
manlio at synapsis:~/projects/tests/nevow$ python static.py
344.71 usec/pass
On a Dell Inspiron 6400 with an Intel Duo Core 2 7200 processor.
Here is the Python code:
import timeit
import random
from nevow import inevow, rend, loaders
COLS = 5
ROWS = 20
class Main(rend.Page):
docFactory = loaders.xmlfile("template.xhtml")
def data_header(self, ctx, data):
return range(COLS)
def data_table(self, ctx, data):
return [
[random.random() for j in range(COLS)]
for i in range(ROWS)
]
main = Main()
t = timeit.Timer("main.renderSynchronously()",
"from __main__ import main")
print "%.2f usec/pass" % (10000 * t.timeit(number=1000) / 1000)
and the template:
<!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"
xmlns:n="http://nevow.com/ns/nevow/0.1"
lang="en" xml:lang="">
<head>
<title>Test Nevow performance</title>
</head>
<body>
<table>
<caption>RandomTable</caption>
<thead>
<tr n:render="sequence" n:data="header">
<th n:pattern="item" n:render="string" />
</tr>
</thead>
<tbody n:render="sequence" n:data="table">
<tr n:pattern="item" n:render="sequence">
<td n:pattern="item" n:render="string" />
</tr>
</tbody>
</table>
</body>
</html>
Now here is the Django code:
import timeit
import random
from twisted.python import util
from django.conf import settings
# Ugh!
settings.configure(TEMPLATE_DIRS=[util.sibpath(__file__, '')])
from django.shortcuts import render_to_response
from django.db import connection
COLS = 5
ROWS = 20
def view():
header = range(COLS)
table = [
[random.random() for j in range(COLS)]
for i in range(ROWS)
]
return render_to_response('template.xhtml',
{"table": table, "header": header})
t = timeit.Timer("view()", "from __main__ import view")
print "%.2f usec/pass" % (10000 * t.timeit(number=1000) / 1000)
and the template:
<!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"
xmlns:n="http://nevow.com/ns/nevow/0.1"
lang="en" xml:lang="en">
<head>
<title>Test Django performance</title>
</head>
<body>
<table>
<caption>RandomTable</caption>
<thead>
<tr>
{% for i in header %}
<th>{{ i }}</li>
{% endfor %}
</tr>
</thead>
<tbody>
{% for i in table %}
<tr>
{% for j in i %}
<td>{{ j }}</td>
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>
</body>
</html>
Regards Manlio Perillo
More information about the Twisted-web
mailing list