Speed ​​comparison: created by HTML server and templates?

I am using the Django version of the Google App Engine in Python.

Is there a significant performance difference between setting loops in a template and putting them in python page handlers?

For example, I am comparing something like this:

{% for i in items %} <div id="item_{{i.key}}"> {{i.text}} </div> {% endfor %} 

Something like this inside my Python code:

 def returnHtml(items): item_array = [] for i in items: item_array.append("<div id='item_%s'>%s</div>" % (i.id, i.text) return "".join(item_array) 

... which is then directly inserted into the django template in the tag, for example:

 {{ item_html }} 

This is a trivial example, realistic, I have more complex loops inside loops, etc. I like to put logic in python code because it is much easier to maintain. But I'm worried about the impact on performance.

Any thoughts? Thanks.

+4
source share
4 answers

The loss of readability and maintainability of your code probably outweighs any performance gains you get. You can find many tests for Python template engines. All popular template engines work reasonably well.

If you don't like the flaws in django templates, use something better. I personally use (and highly recommend) Mako , and I know a few others who like Jinja2 .

+5
source

If you compare this, I am sure that you will find some difference, but I would say that it does not matter at all. The difference in download time for each user is likely to be less than a blink of an eye. I don’t think anyone will notice.

On the other hand, nothing prevents you from compiling the template before deploying it, which should give you almost the same performance as a loop in the code when you run it.

Basically do what makes your life easier in this case ... at GAE, your time will be better spent modeling your data correctly, reducing the number of trips to the data warehouse, etc.

+2
source

Think about what indicators are listed here .

+1
source

I do not think so.

The only thing I can see with real value is that one method can transfer the result to the browser, and not first create the entire page in memory. This can make a difference for huge pages. I'm not familiar enough with Django to find out if a partial result handles a template mechanism or not.

0
source

Source: https://habr.com/ru/post/1338368/


All Articles