Django: it is sometimes faster to use built in python function for model filtering
In a heavy loop, you might want to change django model filtering to built in python filter function.
from myStuff.models import MyModel myModelItems = MyModel.objects.filter(pk__gt = 15) for x in range(1, 500): myModelItems.filter(pk__gte = random.randint(15, 35)).pk
now this is of course bogus code, but it shows the point:
- we have a loop that will execute code many times
- we have same model items selected many times
In this case it is better to use built in python function:
from myStuff.models import MyModel myModelItems = MyModel.objects.filter(pk__gt = 15) for x in range(1, 500): filter(lambda x : x. pk >= random.randint(15, 35), myModelItems).pk
Because django-filter will return new objects and it will have to hit the database every time, while built in function will return the same object and if it was already used, it will not have to reconsult the database.
Definning first segment as a and second as b gives these results:
timeit(a, 5) #1.0679619312286377 timeit(b, 5) #0.19536900520324707
and it gets faster and faster if range is increased.