-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathbenchmark_big_table.py
More file actions
48 lines (41 loc) · 1.23 KB
/
Copy pathbenchmark_big_table.py
File metadata and controls
48 lines (41 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import tempfile
import time
from pathlib import Path
import django
from django.conf import settings
from django.template import Context
from django.template import Template as DjangoTemplate
from jinja2 import Template as JinjaTemplate
from htpy import table, tbody, td, th, thead, tr
settings.configure(TEMPLATES=[{"BACKEND": "django.template.backends.django.DjangoTemplates"}])
django.setup()
django_jinja_template = """
<table>
<thead><tr><th>Row #</th></tr></thead>
<tbody>
{% for row in rows %}
<tr><td>{{ row }}</td></tr>
{% endfor %}
</tbody>
</table>
"""
tests = [
(
"htpy",
lambda rows: str(table[thead[tr[th["Row #"]]], tbody[(tr[td[row]] for row in rows)]]),
),
(
"django",
lambda rows: DjangoTemplate(django_jinja_template).render(Context({"rows": rows})),
),
("jinja2", lambda rows: JinjaTemplate(django_jinja_template).render(rows=rows)),
]
rows = list(range(50_000))
tmp = Path(tempfile.mkdtemp())
for name, func in tests:
start = time.perf_counter()
output = func(rows)
result = time.perf_counter() - start
out_path = tmp / f"{name}_table.html"
out_path.write_text(output)
print(f"{name}: {result} seconds - {out_path}")