4

I'm using Google App Engine's version of Django templates in Python.

Is there a major performance difference between putting loops in the template vs putting it in the python page handlers?

For example, I'm comparing something like this:

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

Vs 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 then gets directly inserted into a django template in a tag like:

{{ item_html }}

This is a trivial example, realistically, I've got more complex loops inside of loops, etc. I like putting the logic inside of the python code because it's much easier to maintain. But I'm worried about the impact on performance.

Any thoughts? Thanks.

Cuga
  • 17,668
  • 31
  • 111
  • 166

4 Answers4

5

The loss in readability and maintainability of your code probably outweigh any performance gains you'll get. You can find many benchmarks of Python template engines. All of the popular template engines perform acceptably.

If you do not like the shortcomings in django templates, use something better. I personally use (and highly recommend) Mako and I know several others who like Jinja2.

Community
  • 1
  • 1
Robert Kluin
  • 8,282
  • 25
  • 21
  • 2
    This is the correct answer. Generating HTML directly instead of using templates is premature optimization of the worst sort. – Nick Johnson Feb 07 '11 at 00:01
2

If you benchmark it, I'm sure you will find some kind of a difference, but I'd say it isn't important at all. The difference in loading time for each user is likely to be less than the blink of an eye. I don't think anyone would notice.

On the other hand, nothing stops you from compiling the template before you deploy it, which should give you pretty much the same performance as looping in code when it runs.

Basically do whatever makes your life easier in this case... on GAE your time will be better spent modelling your data well, cutting down the number to trips to the datastore, etc.

Sudhir Jonathan
  • 16,998
  • 13
  • 66
  • 90
  • Thanks. I'll benchmark eventually. Right now, I'm trying to start out on the right path. I'm not worried so much about page load speed as I am about cpu cycles, etc. – Cuga Feb 04 '11 at 05:30
  • Then you could try compiled templates - they ought to use the same number (or possibly even less) of cpu cycles then doing it manually. – Sudhir Jonathan Feb 04 '11 at 07:04
1

Consider take a look at benchmarks reported here.

  • Instead of just posting a link, it's best to summarize the content. That way readers don't have to click through, and when the link rots the information won't be lost. – bstpierre Sep 30 '12 at 01:54
0

I don't think so.

The only thing that I can see making a real difference is if one method could stream the result out to the browser as opposed to building the complete page in memory first. This could make a difference for huge pages. I am not familiar enough with Django to know if the template engine streams partial results or not.

Thilo
  • 257,207
  • 101
  • 511
  • 656