问题
I'm trying to tweak an application that is suffering in speed department. Because of that, I've started converting all the for-loop statements to list comprehensions when possible.
Currently, I'm working on a function that needs to iterate through a dictionary of Django querysets. The old code uses for-loop statement to iterate through this and it works fine. My code that uses list comprehension returns django querysets instead of my model object.
Here is the code:
def get_children(parent):
# The following works
children = []
for value in get_data_map(parent).itervalues():
children += list(value)
# This part doesn't work as intended.
booms = [value for value in get_data_map(parent).itervalues() if value]
import pdb
pdb.set_trace()
(Pdb) type(children[0])
<class 'site.myapp.models.Children'>
(Pdb) type(booms[0])
<class 'django.db.models.query.QuerySet'>
Note that get_data_map returns a dictionary whose values are <class 'django.db.models.query.QuerySet'>
This part of code is one of the most time consuming part of the application. If I get this working on list comprehensions, the application speed will hopefully get faster by two times.
Any idea how I can speed up this part of the code?
回答1:
Your problem isn't for
loops vs. list comprehensions (better would be generators). Your problem is way too many queries to the database.
Since you're trying to get one list, you should be trying to get it from one query. If you explained what was in your typical QuerySet, we could show you how best to merge them. Maybe use OR
merges on Q objects. Or maybe building up a set of integers and feeding it to an __in=
filter.
回答2:
Your solution does not work because you're creating a list of querysets. Each value is a queryset, and you join them nowhere. Compare:
a = [1,2,3]
b = [x for x in a if x ]
You would expect b to be a list of integers as well, right? You get the list of querysets (better than the list of parents), but you still need to join them. You can do this using itertools.chain:
http://docs.python.org/library/itertools.html#itertools.chain
foos = itertools.chain(booms)
foos should be what you want.
回答3:
Agree it's tough to tell what's going on exactly, but debugging django speed issues is made vastly easier by using the django debug toolbar:
https://github.com/django-debug-toolbar/django-debug-toolbar
If the issue is too many db hits, watch the "SQL queries" tab on the toolbar, it will show it very clearly.
来源:https://stackoverflow.com/questions/6714375/using-list-comprehension-instead-of-for-loop-when-working-with-django-querysets