banx
banx

Reputation: 4416

django templates performance

Which is more efficient in Django: to render an object using the template language? or to push the code to render the object via a templatetag function using the python language?

Upvotes: 4

Views: 730

Answers (3)

Chris Pratt
Chris Pratt

Reputation: 239290

This is like the argument about the most efficient way to append strings (virtually every programming language has that argument). This is 2011, not 1970. Even consumer level computers have processing power and memory reserves to match the supercomputers of the last decade. Network server-class machines have much more. Saving a cycle or two on the processor these days is pretty meaningless. It's one thing when you're developing OS system code or low-level system processes, but for parsing a template, you're wasting your time.

Upvotes: 3

Elf Sternberg
Elf Sternberg

Reputation: 16361

The question is sort-of nonsensical. Templates are always going to be slower than a hand-tuned rendering solution, but template tags still need to run through the Template machinery, so you lose out any advantage there. If you want ultra-high efficiency, consider writing all of the HTML as an array of Python strings, join() them once and then pass your HTML back as the body of an HTTPResponse object.

Or, you could try all three and profile them. Since we don't know your code, we can't do that work for you. After a couple of experiments, you should be satisfied with which approach is correct for you.

The templating engine is almost never your bottleneck. You database is your most likely bottleneck. You do have the Django Toolbar installed, right? If the templating engine is not where your performance is at issue, always use the solution that cheapest for you to implement.

Upvotes: 3

Gabi Purcaru
Gabi Purcaru

Reputation: 31524

I'd say it's about the same; take the most clean way, and if you need speed, cache it.

Upvotes: 1

Related Questions