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/
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. 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. 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. 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. New Relic
● analyze the application
● help understand the stories of application
data
● real-time business insights
http://newrelic.com/
22. 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
23. Reducing Queries
● Use of Class Based Views
● proper use of query sets
● select_related() and prefetch_related()