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.

5 Vegan Super Foods You Will Find in Any Supermarket

December 28, 2013

Here is the post about vegan super foods you should eat everyday. The best part is — they are really cheap and you will have no trouble finding them in your closest supermarket!

B12 is not the only vitamin vegans will not get naturally with their food

December 27, 2013

Recently I’ve published an article about vitamins called B12 is not the only vitamin vegans will not get naturally with their food, it is about vitamin D, where to get it and why we need it.

Vegan Diet for Vegan Athlete

December 26, 2013

For the past few months I’ve been working on my project for vegans called Vegan Muscles – Vegan Diet for Vegan Athlete.

In the future Vegan Muscles will create personalized vegan diet based on activity level and goals. It will calculate daily intake for carbs, proteins and fats as well as every amino acid, mineral and vitamin and suggest weekly recipes.

It is still in quite early development mode so it is still manual, but more features are coming really soon! As for now you can use Vegan Muscles to see if you are not missing any vital nutrients, or read vegan blog full of tips for vegan nutrition. It is quite young blog too, but articles have been accepted by vegan community quite well.

Go Vegan – Stay Active!

Hackers & Painters review

November 4, 2012

Hackers and Painters : Big Ideas from the Computer Age is a collection of essays written by Paul Graham. I think you can find them all on his personal web page here:

http://www.paulgraham.com/

If you are not planning on buying this book, I at least highly recommend to read the essays online. They are awesome, but lets get ourselves there. After all, I’m writing a review.

Paul Graham

First lets talk about Paul Graham himself. Not in a sense of his bio, you can find it on his website. But about him as a person. Of course, he might be completely different, but this is how I see him through the book and online essays.

He is a hacker, LISP evangelist, OOP critic and in fact a critic in general. That is the most important. He is a critic and skeptic of all the things.

He is also very opinionated and together with his skepticism, it makes this book really great. I could easily say that he has a strong opinion about any topic. The book is about computers in general, but Paul will take a chance to state his opinion not only on that. One example could be his view on social issues. We can all remember how jobless young people flooded the streets of NYC in 2012 and later all over the world (with the 99% bullshit). What they were protesting against is what Paul Graham states as a sign of healthy society. And he is completely right.

One of his chapters is called “What You Can’t Say” and throughout the book he always says what is the opposite of what the majority would believe. Sometimes I would agree with him and read with a satisfied smile on my face seeing how he crushes those that “know wrong”. Sometimes I would disagree and would read with other kind of pleasure thinking of arguments I could use against him.

The Book

The book is written in a very great style. It is almost as having a real conversation with author, not just reading. It got me smiling, laughing and grinding my teeth. But most importantly it got me thinking numerous times. I’ve read it mostly on the bus and at least half of the time I was not actually reading. I was looking through the window playing with ideas and memories that were generated by the words written in the pages. If this is not a sign of awesome book, I don’t know what is.

Who should read it? Well, hackers of course. Even if essays are quite old (the book was published in 2004 and essays written even a longer time ago), the great ideas are time-proof and the book is full of them. It will inspire, motivate and overall make you a better programmer.

Anyone else? Probably those that have to deal with hackers on the daily basis. You know: wives, girlfriends, husbands, boyfriends. This book reveals how the brains of a hacker work. Not all chapters of course (programming languages are probably not very interesting to those who don’t code), but some of them are highly recommended. So if you have a hard time understanding your life partner who happens to be a hacker, give it a go. I’d recommend reading first 7 chapters.

But wait, you might say, there is another kind of people that deal with hackers on the daily basis. Technical people that don’t know anything about technology. You know, most of the project managers and software analysts. To be fair, even most of programmers. Those that should be good at understanding how things work down there but really don’t. Should they read it? Without a doubt. If they would listen to the words in this book, real hackers will start to hate them less, projects will fail less and in result we all would deliver a better software.

Generating Tiles With QGIS

November 1, 2012
tags: , ,

Finally I’ve committed to start a project for generating Tiles with Qgis, if you’d like to fallow, here it is on GitHub:

https://github.com/mykk/quantumTiles

It only has a very hardcoded file I used to generate files outside Qgis, but eventually I hope it’ll turn to a useful Qgis plugin.

Learn New Language Every Year

September 19, 2012

Why not INTERCAL? Ever heard of Mortgage Code? It is writing code so bizarre and complex that only you can (hardly) understand what’s going on, thus your employer will be too feared to lay you off, keeping you safe while you’re on that loan.

Well INTERCAL takes it to the whole new level. Here is a fine paper on this programming language:

http://www.catb.org/~esr/intercal/paper.html

And here is a story behind it:

Alexander Garrett wrote a paper on INTERCAL for his Spring 1997 Programming Languages: Theory and Design class. Alexander writes:

The obvious choice was INTERCAL (I’m still quite surprised that I’m the only one who picked it — most people did Java??). Anyway, it was not favourably received…when [the professor] handed it back, he said, “Ah. I see you’re someone with a sense of humour. Unfortunately for you, I’m not.”

taken from here http://www.catb.org/~esr/intercal/

Have fun supporting your new investment!

Follow

Get every new post delivered to your Inbox.