faster python/django programming

27
Faster Programming in Python/Django Subit Raj Pokharel @ rajsubit, rajsubit

Upload: raj-subit

Post on 02-Jul-2015

533 views

Category:

Technology


1 download

DESCRIPTION

The slide consists some tips about good programming practices in python/django as well as some tools and techniques to make application faster.

TRANSCRIPT

Page 1: Faster python/django programming

Faster Programming inPython/Django

Subit Raj Pokharel@ rajsubit, rajsubit

Page 2: Faster python/django programming

First Code then optimize

Page 3: Faster python/django programming

Response Time Limits● 0.1 seconds

○ limit for having the user feel that the system is reacting instantaneously

● 1 seconds○ limit for the user's flow of thought to stay uninterrupted

● 10 seconds○ limit for keeping the user's attention focused on the dialogue

http://www.nngroup.com/articles/response-times-3-important-limits/

Page 4: Faster python/django programming

Time consuming function

New Relichttp://newrelic.com/

Page 5: Faster python/django programming

def find_recent_blog_post():...url = requests.get("http://blog.flipkarma.com/feed")soup = BeautifulSoup.BeautifulSoup(url.text)recent_post = soup.findAll('item')[:2]...

A simple example

Page 6: Faster python/django programming

Analysis with Line ProfilerLine # Hits Time Per Hit % Time Line Contents============================================================== 70 1 1112952 1112952.0 73.4 url = requests.get("http://blog.flipkarma.com/feed") 71 1 103695 103695.0 6.8 soup = BeautifulSoup.BeautifulSoup(url.text) 72 1 2186 2186.0 0.1 recent_post = soup.findAll('item')[:2]

https://github.com/rkern/line_profiler$ pip install line_profiler

Page 7: Faster python/django programming

Optimization with Memcached# Original codeurl = requests.get("http://blog.flipkarma.com/feed")soup = BeautifulSoup.BeautifulSoup(url.text)recent_post = soup.findAll('item')[:2]

# Using Memcachedkey = "flipkarma_blog_post"cache_time = 10000result = cache.get(key)if result: recent_post = resultelse:

url = requests.get("http://blog.flipkarma.com/feed")soup = BeautifulSoup.BeautifulSoup(url.text)recent_post = soup.findAll('item')[:2]cache.set(key, recent_post, cache_time)

http://memcached.org/

Page 8: Faster python/django programming

Line # Hits Time Per Hit % Time Line Contents============================================================== 65 1 3 3.0 0.0 cache_time = 10000 # time to live in seconds 66 1 17973 17973.0 1.3 result = cache.get(cache_key) 67 1 4 4.0 0.0 if result: 68 1 31 31.0 0.0 parameters["result"] = result 69 1 3 3.0 0.0 if not result: 74 url = requests.get("http://blog.flipkarma.com/feed")

soup = BeautifulSoup.BeautifulSoup(url.text)

recent_post = soup.findAll('item')[:2]

cache.set(cache_key, recent_post, cache_time)

Result

Page 9: Faster python/django programming

New Relic

● analyze the application● help understand the stories of application

data● real-time business insights

http://newrelic.com/

Page 10: Faster python/django programming
Page 11: Faster python/django programming

Profiling

Python Tools:● cProfile● profile● hotshot

Django Debug ToolbarLine Profiler

Page 12: Faster python/django programming

Django Debug Toolbar

Page 13: Faster python/django programming

SQL Query

Page 14: Faster python/django programming

Query Time

Page 15: Faster python/django programming

Reducing Query

Page 16: Faster python/django programming

Query Time

Page 17: Faster python/django programming

Some Tips

Page 18: Faster python/django programming

If it's fast enough, don't optimise it.

Page 19: Faster python/django programming

Find the slowest step first.

Page 20: Faster python/django programming

Make the slowest operation faster

● Python functions have an overhead. Inline if possible● List comprehensions are faster than for or if

Page 21: Faster python/django programming

Reduce the number of hits.

Page 22: Faster python/django programming

Perform each operation as rarely as possible.

● Cache the result if speed is more important than memory● Move slower operations inside if conditions● assign operation to variable only if necessary

Page 23: Faster python/django programming

Reducing Queries

● Use of Class Based Views● proper use of query sets● select_related() and prefetch_related()

Page 24: Faster python/django programming

Change the algorithm. This has the second-biggest impact on speed

Page 25: Faster python/django programming

The largest impact comes from eliminating code.

Page 26: Faster python/django programming

Functionality is an asset. Code is a liability.

Page 27: Faster python/django programming

Thank You