#35332: Bad performance in django.template.load.render_to_string
-------------------------------------+-------------------------------------
Reporter: Zeyad | Owner: nobody
Moustafa |
Type: | Status: new
Uncategorized |
Component: Template | Version: 5.0
system | Keywords: performance
Severity: Normal | templates jinja
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
Hi folks, thanks for your great job here.
So with the rise of rust we are seeing, I told myself to make a component
of Django components in rust so we can get a better performance as memory
safety and all of these features from rust.
While searching I discovered a great package called mini-jinja. so I just
wanted to compare its performance with the Django template engine
performance to figure out if what I am doing is good for the community or
not. so here is the code I have written to know about the performance.
{{{
# mini-jinja.py
from minijinja import Environment
with open('test.html', 'r') as file:
template = file.read()
env = Environment(
templates={
'test': template
}
)
result = env.render_template('test', nums=range(1000000))
}}}
and this is the result of the testing.
{{{
❯ time python mini-jinja.py
python mini-jinja.py 1.27s user 0.03s system 99% cpu 1.302 total
dummy/python-tests/copper via 🐍 v3.10.12 (.venv)
❯ time python mini-jinja.py
python mini-jinja.py 1.23s user 0.03s system 99% cpu 1.259 total
}}}
and this is how I tested it in django
first created main.py file with this contents
{{{
import time
from django.template.loader import render_to_string
def render():
start = time.time()
_template = render_to_string("test.html", {"nums": range(1000000)})
end = time.time()
print(end - start)
}}}
and then I opened the django shell and imported the main file and then
typed render and here is what I got
{{{
❯ python manage.py shell
Python 3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> import main
>>> main.render()
28.39499545097351
>>>
}}}
and here is the test.html I was using for both the tests
{{{
{% for num in nums%}
<h1>{{num}}</h1>
{% endfor %}
}}}
So here is my question. why a big difference between both of them?? is
there anything I have done wrong or this is related to the nature of
python?? I just opened this issue to know if it will be really good for
the Django community to create a template engine using rust built on top
of Mini-jinja or if there is something I have to modify in my tests to get
the real result??
--
Ticket URL: <https://code.djangoproject.com/ticket/35332>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/0107018e78fbdc89-f94550f8-e5bd-41db-98cb-5ed26d06a30a-000000%40eu-central-1.amazonses.com.