Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Faster Programming in
Python/Django
Subit Raj Pokharel
@ rajsubit,
rajsubit
First Code then optimize
Response Time Limits
● 0.1 seconds
○ limit for having the user feel that the system is reacting
instantaneously
● 1 second...
Time consuming function
New Relic
http://newrelic.com/
def find_recent_blog_post():
...
url = requests.get("http://blog.flipkarma.com/feed")
soup = BeautifulSoup.BeautifulSoup(u...
Analysis with Line Profiler
Line # Hits Time Per Hit % Time Line Contents
================================================...
Optimization with Memcached
# Original code
url = requests.get("http://blog.flipkarma.com/feed")
soup = BeautifulSoup.Beau...
Line # Hits Time Per Hit % Time Line Contents
==============================================================
65 1 3 3.0 0....
New Relic
● analyze the application
● help understand the stories of application
data
● real-time business insights
http:/...
Profiling
Python Tools:
● cProfile
● profile
● hotshot
Django Debug Toolbar
Line Profiler
Django Debug Toolbar
SQL Query
Query Time
Reducing Query
Query Time
Some Tips
If it's fast enough, don't optimise it.
Find the slowest step first.
Make the slowest operation faster
● Python functions have an overhead. Inline if possible
● List comprehensions are faster...
Reduce the number of hits.
Perform each operation as rarely as
possible.
● Cache the result if speed is more important than memory
● Move slower oper...
Reducing Queries
● Use of Class Based Views
● proper use of query sets
● select_related() and prefetch_related()
Change the algorithm.
This has the second-biggest impact on speed
The largest impact comes from
eliminating code.
Functionality is an asset. Code is a
liability.
Thank You
Faster python/django programming
Upcoming SlideShare
Loading in …5
×

Faster python/django programming

1,115 views

Published on

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

Published in: Technology

Faster python/django programming

  1. 1. Faster Programming in Python/Django Subit Raj Pokharel @ rajsubit, rajsubit
  2. 2. First Code then optimize
  3. 3. 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/
  4. 4. Time consuming function New Relic http://newrelic.com/
  5. 5. 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
  6. 6. Analysis with Line Profiler Line # 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
  7. 7. Optimization with Memcached # Original code url = requests.get("http://blog.flipkarma.com/feed") soup = BeautifulSoup.BeautifulSoup(url.text) recent_post = soup.findAll('item')[:2] # Using Memcached key = "flipkarma_blog_post" cache_time = 10000 result = cache.get(key) if result: recent_post = result else: 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/
  8. 8. 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
  9. 9. New Relic ● analyze the application ● help understand the stories of application data ● real-time business insights http://newrelic.com/
  10. 10. Profiling Python Tools: ● cProfile ● profile ● hotshot Django Debug Toolbar Line Profiler
  11. 11. Django Debug Toolbar
  12. 12. SQL Query
  13. 13. Query Time
  14. 14. Reducing Query
  15. 15. Query Time
  16. 16. Some Tips
  17. 17. If it's fast enough, don't optimise it.
  18. 18. Find the slowest step first.
  19. 19. Make the slowest operation faster ● Python functions have an overhead. Inline if possible ● List comprehensions are faster than for or if
  20. 20. Reduce the number of hits.
  21. 21. 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
  22. 22. Reducing Queries ● Use of Class Based Views ● proper use of query sets ● select_related() and prefetch_related()
  23. 23. Change the algorithm. This has the second-biggest impact on speed
  24. 24. The largest impact comes from eliminating code.
  25. 25. Functionality is an asset. Code is a liability.
  26. 26. Thank You

×