Re: slow function

2011-12-05 Thread Håkon Erichsen
2011/12/5 kenneth gonsalves 

>  The code looks simple to me, but to analyse 10,000 rounds of golf
> (1,80,000 hole scores) my 2 GB laptop takes 20 minutes. Would this be
> considered normal - or slow? If slow, ideas on how to speed it up would be
> appreciated.
>
>
One thing you should look into is QuerySet.select_related():
https://docs.djangoproject.com/en/dev/ref/models/querysets/#select-related

Without it, every time you run scr.hole on a new scr, Django will run a new
query, fetching that 'hole' object. If you have 10 000 scores, that means
10 000 queries, which is an insane number for queries like this. If you use
select_related, Django will fetch the corrosponding hole for you, in the
same query... meaning 1 query, instead of 10 000 :)

In other words, change this:


scrs = Score.objects.all()pscrs =
Pscore.objects.filter(hole__tee__course = club)


To this:


scrs = Score.objects.all().select_related('hole')pscrs =
Pscore.objects.filter(hole__tee__course = club).select_related('hole')


That seems to be the biggest problem you have. Some other comments:

- Holy mother of god, that's a huge view file! I would advice to slice
it into logically separated files, and put this in a directory called
"views", everything doesn't have to be in views.py!

- Put your forms in another file, like forms.py

- If you're just checking if someone is logged in with
@user_passes_test, why not just use @login_required?

- Check out render:
https://docs.djangoproject.com/en/dev/topics/http/shortcuts/#render


Best regards,

Håkon Erichsen

-- 
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 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: slow function

2011-12-05 Thread Håkon Erichsen
2011/12/5 Brian Schott 

> What's the best dangothonic way to break up models.py or views.py for that
> matter.
> 1. Create a models.py or views.py at the top level that does a bunch of
> imports?  Explicitly or progrmmatically by looping over files?
> 2. Expose the hierarchy to the caller.  Ex: from my_app.models.my_model
> import MyModel
> 3. Break up the whole app into smaller apps?
>
> Still trying to figure out a good way to determine what should be a
> separate app.  If you have a relathionships like Publishers -> Books, and
> Authors -> Books should you try to break out Books, Authors, and Publishers
> as separate apps?
>

For views, I think the most djangoic (?) way is making a *app*/views/
package, and then put separate files there. You can import the views in
 *app*/views/__init__.py if you want, but it's really not necessary: I find
that e.g. "from user.views.permissions import edit" looks pretty well, and
the django admin does this as well.

For models: See https://code.djangoproject.com/ticket/4470 and
https://code.djangoproject.com/ticket/3591 .  (In other words: Currently
not possible in a pretty way).The currently best method is probably the one
with a _models directory, or defining the app_label. but this might be
fixed soon, the tickets have som recent activity :)

Best regards,
Håkon Erichsen

-- 
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 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: slow function

2011-12-06 Thread Håkon Erichsen
2011/12/6 kenneth gonsalves 

>
> I first did select_related() and the laptop nearly caught fire! timed
> out after an hour or so. I then did select_related(depth=1) and it took
> less than a minute.
>

Indeed, select_related() will follow every foreignkey it gets to, which can
be a lot, so you either have to specify a field like I did ('hole'), or a
depth. Glad it worked!


> there is a lot of duplicated code - a huge clean up is needed, but every
> time I sit to do it, a new feature is needed, so it does not get done!
>

Ah, the usual plague. Nothing is as permanent as a hack that works ;)

will do - thanks to all for taking the trouble of reading the code and
> making suggestions.


It was just some thoughts after a quick scan, not really any trouble.
Actually, I should be sorry for the unsolicited advice, I'm glad you took
it the right way! ;)

Best regards
Håkon Erichsen

-- 
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 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.