faster python/django programming
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](https://reader033.vdocuments.site/reader033/viewer/2022042518/559446301a28ab0e738b4633/html5/thumbnails/1.jpg)
Faster Programming inPython/Django
Subit Raj Pokharel@ rajsubit, rajsubit
![Page 2: Faster python/django programming](https://reader033.vdocuments.site/reader033/viewer/2022042518/559446301a28ab0e738b4633/html5/thumbnails/2.jpg)
First Code then optimize
![Page 3: Faster python/django programming](https://reader033.vdocuments.site/reader033/viewer/2022042518/559446301a28ab0e738b4633/html5/thumbnails/3.jpg)
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 5: Faster python/django programming](https://reader033.vdocuments.site/reader033/viewer/2022042518/559446301a28ab0e738b4633/html5/thumbnails/5.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022042518/559446301a28ab0e738b4633/html5/thumbnails/6.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022042518/559446301a28ab0e738b4633/html5/thumbnails/7.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022042518/559446301a28ab0e738b4633/html5/thumbnails/8.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022042518/559446301a28ab0e738b4633/html5/thumbnails/9.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022042518/559446301a28ab0e738b4633/html5/thumbnails/10.jpg)
![Page 11: Faster python/django programming](https://reader033.vdocuments.site/reader033/viewer/2022042518/559446301a28ab0e738b4633/html5/thumbnails/11.jpg)
Profiling
Python Tools:● cProfile● profile● hotshot
Django Debug ToolbarLine Profiler
![Page 12: Faster python/django programming](https://reader033.vdocuments.site/reader033/viewer/2022042518/559446301a28ab0e738b4633/html5/thumbnails/12.jpg)
Django Debug Toolbar
![Page 13: Faster python/django programming](https://reader033.vdocuments.site/reader033/viewer/2022042518/559446301a28ab0e738b4633/html5/thumbnails/13.jpg)
SQL Query
![Page 14: Faster python/django programming](https://reader033.vdocuments.site/reader033/viewer/2022042518/559446301a28ab0e738b4633/html5/thumbnails/14.jpg)
Query Time
![Page 15: Faster python/django programming](https://reader033.vdocuments.site/reader033/viewer/2022042518/559446301a28ab0e738b4633/html5/thumbnails/15.jpg)
Reducing Query
![Page 16: Faster python/django programming](https://reader033.vdocuments.site/reader033/viewer/2022042518/559446301a28ab0e738b4633/html5/thumbnails/16.jpg)
Query Time
![Page 17: Faster python/django programming](https://reader033.vdocuments.site/reader033/viewer/2022042518/559446301a28ab0e738b4633/html5/thumbnails/17.jpg)
Some Tips
![Page 18: Faster python/django programming](https://reader033.vdocuments.site/reader033/viewer/2022042518/559446301a28ab0e738b4633/html5/thumbnails/18.jpg)
If it's fast enough, don't optimise it.
![Page 19: Faster python/django programming](https://reader033.vdocuments.site/reader033/viewer/2022042518/559446301a28ab0e738b4633/html5/thumbnails/19.jpg)
Find the slowest step first.
![Page 20: Faster python/django programming](https://reader033.vdocuments.site/reader033/viewer/2022042518/559446301a28ab0e738b4633/html5/thumbnails/20.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022042518/559446301a28ab0e738b4633/html5/thumbnails/21.jpg)
Reduce the number of hits.
![Page 22: Faster python/django programming](https://reader033.vdocuments.site/reader033/viewer/2022042518/559446301a28ab0e738b4633/html5/thumbnails/22.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022042518/559446301a28ab0e738b4633/html5/thumbnails/23.jpg)
Reducing Queries
● Use of Class Based Views● proper use of query sets● select_related() and prefetch_related()
![Page 24: Faster python/django programming](https://reader033.vdocuments.site/reader033/viewer/2022042518/559446301a28ab0e738b4633/html5/thumbnails/24.jpg)
Change the algorithm. This has the second-biggest impact on speed
![Page 25: Faster python/django programming](https://reader033.vdocuments.site/reader033/viewer/2022042518/559446301a28ab0e738b4633/html5/thumbnails/25.jpg)
The largest impact comes from eliminating code.
![Page 26: Faster python/django programming](https://reader033.vdocuments.site/reader033/viewer/2022042518/559446301a28ab0e738b4633/html5/thumbnails/26.jpg)
Functionality is an asset. Code is a liability.
![Page 27: Faster python/django programming](https://reader033.vdocuments.site/reader033/viewer/2022042518/559446301a28ab0e738b4633/html5/thumbnails/27.jpg)
Thank You