Getting a low performance in django.template.loader.render_to_string

So I was wondering if anyone knows why I am getting a low performance in Django while rendering a template using render_to_string compared to mini-jinja (a template engine built in rust) is this just related to the fact that Django is built using Python and mini-jinja is built using rust or there are also some reasons I don’t know. because I was wondering if building a template engine in Rust on top of Mini-jinja would be good for the community or not. so here is what I have done to measure 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 here is the time it took here

❯ time python mini-jinja.py
python mini-jinja.py  1.23s user 0.04s system 99% cpu 1.267 total

dummy/python-tests/copper via 🐍 v3.10.12 (.venv)
❯ time python mini-jinja.py
python mini-jinja.py  1.35s user 0.37s system 99% cpu 1.718 total

and here is what I have done in Django

first I made a main.py module with these 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)

then imported it using Django shell and called the render function 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)
>>> from main import render
>>> render()
28.742142915725708
>>>

and here is the template I was using for this test

// test.html
{% for num in nums%}

<h1>{{num}}</h1>

{% endfor %}

so do you think that if I built a template engine on top of mini-jinja will be good or not Do you have any suggestions for it or is there anything wrong with the benchmarking?

It’s well-known that the Django template engine isn’t the fastest possible engine.

However, I don’t think that rendering 1,000,000 of anything to generate a 16 MB web page is a practical test case and doesn’t really measure the effective difference in performance. (Side note: some tiny, miniscule even, amount of time that you’re measuring here includes the overhead of the template search facility to locate the template file.)

Regardless, Django does provide the facilities for allowing you to substitute rendering engines. See How to implement a custom template backend | Django documentation | Django

If you really are in a case where the rendering performance matters, I’d suggest you check out the Jinja2 engine to see if that improves the situation for you. (See Templates | Django documentation | Django for more information.)

Here is my situation. I want to build a template engine for Django in rust so I want to know if it’s a good thing for the community or it will not affect it a lot so I wanted to see the performance. now if there is anything wrong with the way I am using to see the performance just tell me a better way to measure it.

And another thing is that I know about what django provides in terms of creating a custom engine and this is what I am gonna use.

So In summary I want to know your opinion about a django template engine built in rust and also about the better way to measure the performance.

Having options is always a good thing

Contrived examples are a poor basis for conducting benchmarks. They are not representative of actual results you can expect to achieve. Also, those benchmarks need to be run in an environment comparable to your production environment. (As a side note, I replicated your Django test locally here on my laptop. Multiple iterations of the test all completed in less than 8 seconds per test - with the average being about 7.75 seconds, certainly nothing anywhere close to the near-29 seconds you’ve reported. So environmental and version considerations are also going to have a significant effect on results.)

Try testing more realistic templates. Take one of your projects, select one of the templates and measure the time required for the Django engine to render it. Then try rendering that template using your alternate engine, and compare the results. Then you need to decide if the difference in time is significant to where a replacement is necessary in practical terms.

If it were really a difference between rendering in 1 second vs 10 seconds, it would clearly be worth addressing. But what if the difference is 1 ms vs 10 ms? In 99+% of the cases, you’re never going to notice the difference. (Some checking of a couple templates that I’m using puts the average rendering time of those templates at less than 4 ms each.)

Are there people who would benefit from a higher performing template engine? I’m sure there are. But I am not one of them. So if you’re asking for my opinion, no, I don’t see a value to it. But I am just one individual expressing an opinion.

Thanks really for your answer it helped me a lot. I will take the opinion of some devs I know and then I might build such a thing