Re: Designing for Speed - conditionals inside the view versus the template

2007-03-12 Thread Ned Batchelder
And unless your application is extremely unusual, by far the biggest 
expense is database queries.  More than anything else, this is what 
governs the speed of your application.  So don't worry about where the 
conditionals are being evaluated: worry about how much data you are 
pulling from the database.

Keep in mind that Django's ORM has a lazy evaluation model, so your view 
code could load a context variable with a list of objects from the 
database, and if the template never uses the variable, then the database 
query is never executed.  This means that it can be difficult to 
determine how many queries are really executing unless you actually 
trace the database traffic.

--Ned.

John DeRosa wrote:
> Merric Mercer wrote:
>   
>> I have  view that returns a whole bunch of different variables and 
>> boolean flags.The state of these flags determine what the user 
>> actually sees on their screen.
>>
>>  From a design perspective I seem to have two options:-
>>
>>
>> 1.   Design a template with lots of {% if %} conditional statements to 
>> check the status of various flags, in order to display the appropriate 
>> information to the user.
>>
>> OR
>>
>> 2.   Do the bulk of the work in the view,  by doing something like:-
>> 
> [snip]
>   
>> 3.  Do a combination of the two above
>>
>> Option number 2 lacks the flexibility of doing stuff in the template, 
>> but would it make a big difference in speed?  I am concerned that having 
>> a lot of conditional statements in the template might be slower than 
>> doing the work in the view.
>>
>> Can anybody shed some light on this and the trade off (if any) of speed 
>> versus flexibility.
>> 
>
> A view is Python code, which is compiled into bytecode and then executed 
> by the Python interpreter.  Whereas a template is interpreted by the 
> Python template engine, which is Python code executed by the Python 
> interpreter.
>
> A template will will always have that extra layer of interpretation. 
> Think of it as a new language with a compiler written in Python.  (Which 
> it is...)  So from the smallest perspective, an if-else in a template 
> will always be slower than the equivalent {%if%}-{%else%}-{%endif%} in a 
> view.
>
> But before you pull out a stopwatch and move all of your application's 
> decisions into the views, think about the long-term maintainability, 
> likelihood of bugs, the goodness of separating of "what" code from "how" 
> code, etc.  The history of software is littered with code that was 
> extremely efficient but impossible to maintain or adapt to changing 
> requirements.  You can double your application's performance by buying 
> or leasing a new box + more memory next year.  What % improvement do you 
> think code shifting from template to view will get you?
>
> John
>
>
> >
>
>
>
>   

-- 
Ned Batchelder, http://nedbatchelder.com


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Designing for Speed - conditionals inside the view versus the template

2007-03-12 Thread James Bennett

On 3/12/07, Bill de hOra <[EMAIL PROTECTED]> wrote:
> Merric Mercer wrote:
>
> > 1.   Design a template with lots of {% if %} conditional statements
>  >
> > 2.   Do the bulk of the work in the view,  by doing something like:-
> >
> > 3.  Do a combination of the two above
>
> 4: Caching?

5. Start off with just the template, and see how it performs.
Optimizing before you know whether you need to is one of the deadly
sins :)

-- 
"Bureaucrat Conrad, you are technically correct -- the best kind of correct."

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Designing for Speed - conditionals inside the view versus the template

2007-03-12 Thread Bill de hOra

Merric Mercer wrote:

> 1.   Design a template with lots of {% if %} conditional statements
 >
> 2.   Do the bulk of the work in the view,  by doing something like:- 
> 
> 3.  Do a combination of the two above

4: Caching?

cheers
Bill


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Designing for Speed - conditionals inside the view versus the template

2007-03-12 Thread John DeRosa

Merric Mercer wrote:
> I have  view that returns a whole bunch of different variables and 
> boolean flags.The state of these flags determine what the user 
> actually sees on their screen.
> 
>  From a design perspective I seem to have two options:-
> 
> 
> 1.   Design a template with lots of {% if %} conditional statements to 
> check the status of various flags, in order to display the appropriate 
> information to the user.
> 
> OR
> 
> 2.   Do the bulk of the work in the view,  by doing something like:-
[snip]
> 3.  Do a combination of the two above
> 
> Option number 2 lacks the flexibility of doing stuff in the template, 
> but would it make a big difference in speed?  I am concerned that having 
> a lot of conditional statements in the template might be slower than 
> doing the work in the view.
> 
> Can anybody shed some light on this and the trade off (if any) of speed 
> versus flexibility.

A view is Python code, which is compiled into bytecode and then executed 
by the Python interpreter.  Whereas a template is interpreted by the 
Python template engine, which is Python code executed by the Python 
interpreter.

A template will will always have that extra layer of interpretation. 
Think of it as a new language with a compiler written in Python.  (Which 
it is...)  So from the smallest perspective, an if-else in a template 
will always be slower than the equivalent {%if%}-{%else%}-{%endif%} in a 
view.

But before you pull out a stopwatch and move all of your application's 
decisions into the views, think about the long-term maintainability, 
likelihood of bugs, the goodness of separating of "what" code from "how" 
code, etc.  The history of software is littered with code that was 
extremely efficient but impossible to maintain or adapt to changing 
requirements.  You can double your application's performance by buying 
or leasing a new box + more memory next year.  What % improvement do you 
think code shifting from template to view will get you?

John


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Designing for Speed - conditionals inside the view versus the template

2007-03-12 Thread Merric Mercer

I have  view that returns a whole bunch of different variables and 
boolean flags.The state of these flags determine what the user 
actually sees on their screen.

 From a design perspective I seem to have two options:-


1.   Design a template with lots of {% if %} conditional statements to 
check the status of various flags, in order to display the appropriate 
information to the user.

OR

2.   Do the bulk of the work in the view,  by doing something like:-
  if  conditions A:
 template = "template_a.html"
  elif conditions B:
 template = "template_b.html"
  else conditions C:
 template = "template_c.html"

 return render to response(template name,{dictionary_of_variables},)


3.  Do a combination of the two above

Option number 2 lacks the flexibility of doing stuff in the template, 
but would it make a big difference in speed?  I am concerned that having 
a lot of conditional statements in the template might be slower than 
doing the work in the view.

Can anybody shed some light on this and the trade off (if any) of speed 
versus flexibility.

MerMer


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---