Skip to content

Django: it is sometimes faster to use built in python function for model filtering

March 3, 2014

In a heavy loop, you might want to change django model filtering to built in python filter function.

Consider this

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))[0].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)[0].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.

Advertisements
No comments yet

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: