Skip to content

Commit 54af5ba

Browse files
committed
4Performance
1 parent eb1c477 commit 54af5ba

14 files changed

Lines changed: 585 additions & 0 deletions
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
1. common_items.py
2+
2. plot.py
3+
3. primes.py
4+
4. mem_profile_example.py
5+
5. sub_string.py
6+
6. objgraph_example.py
7+
7. rotate.py
8+
8. sub_string2.py
9+
9. defaultdict_example.py
10+
10. ordered.py
11+
11. hound_words.py
12+
12. chainmap_example.py
13+
13. named.py
14+
14. bloomtest.py
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Extra Code
2+
3+
"""
4+
5+
Big-O notation image generator. Code which generated the Big-O
6+
growth graph (order) in the book.
7+
8+
"""
9+
10+
11+
import numpy as np
12+
import math
13+
import matplotlib.pyplot as plt
14+
from functools import reduce
15+
16+
def f(t):
17+
return np.exp(-t) * np.cos(2*np.pi*t)
18+
19+
def k(t):
20+
return [0]
21+
22+
def l(t):
23+
return list(map(math.log, t))
24+
25+
def x(t):
26+
return list(map(lambda x: x, t))
27+
28+
def xl(t):
29+
return list(map(lambda x: math.log(x)*x, t))
30+
31+
def sq(t):
32+
return list(map(lambda x: x*x, t))
33+
34+
def p2(t):
35+
return list(map(lambda x: pow(2,x), t))
36+
37+
def fact(n):
38+
return reduce(lambda x,y : x*y, range(1, n+1))
39+
40+
def facn(t):
41+
return list(map(lambda x: fact(x), t))
42+
43+
t = range(1, 101)
44+
45+
plt.axis([0, 50, 0, 50])
46+
line1=plt.plot(t, [1]*100, '#00ee00')
47+
print(line1)
48+
plt.text(45, 1.5, r'O(1)')
49+
50+
line2=plt.plot(t, l(t), color='#00bb00')
51+
plt.text(40, 4.8, r'O(log(n))')
52+
53+
line3=plt.plot(t, x(t), color='#008800')
54+
plt.text(42, 40, r'O((n))')
55+
56+
line4=plt.plot(t, xl(t), color='#eeee00')
57+
plt.text(18, 45, r'O(nlog(n))')
58+
59+
line5=plt.plot(t, sq(t), color='#440000')
60+
plt.text(8, 44, r'O(n^2)')
61+
62+
line6=plt.plot(t, p2(t), color='#bb0000')
63+
plt.text(2, 35, r'O(2^n)')
64+
65+
line7=plt.plot(t, facn(t), color='#ff0000')
66+
plt.text(0, 45, r'O(n!)')
67+
68+
plt.show()
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Code Listing #12
2+
"""
3+
4+
Example of using bloom filter
5+
6+
NOTE: This works only with Python2.x
7+
8+
"""
9+
10+
from pybloom import BloomFilter
11+
import requests
12+
13+
# Uncomment for profiling with line profiler or memory profiler
14+
15+
# @profile
16+
def hound():
17+
f=BloomFilter(capacity=100000, error_rate=0.01)
18+
text=requests.get('https://www.gutenberg.org/files/2852/2852-0.txt').text
19+
20+
for word in text.split():
21+
word = word.lower().strip()
22+
f.add(word)
23+
24+
print len(f)
25+
print len(text.split())
26+
27+
for w in ('holmes','watson','hound','moor','queen'):
28+
print 'Found',w,w in f
29+
30+
31+
if __name__ == "__main__":
32+
hound()
33+
34+
35+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Code Listing #10
2+
3+
"""
4+
5+
Example of using ChainMap
6+
7+
"""
8+
9+
from collections import ChainMap
10+
11+
d1 = {i:i for i in range(100)}
12+
d2 = {i:i*i for i in range(100)}
13+
c = ChainMap(d1, d2)
14+
# Older value still accessible
15+
print(c[5])
16+
print(c.maps[0][5])
17+
# Updating d1 with d2
18+
d1.update(d2)
19+
print(d1)
20+
# Olde value got updated
21+
print(c[5])
22+
print(c.maps[0][5])
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Code Listing #7
2+
3+
"""
4+
5+
Examples of using defaultdict
6+
7+
"""
8+
9+
from collections import defaultdict
10+
11+
counts = {}
12+
text="""Python is an interpreted language. Python is an object-oriented language.
13+
Python is easy to learn. Python is an open source language. """
14+
word="Python"
15+
16+
# Implementations with simple dictionary
17+
for word in text.split():
18+
word = word.lower().strip()
19+
try:
20+
counts[word] += 1
21+
except KeyError:
22+
counts[word] = 1
23+
24+
print("Counts of word",word,'=>',counts[word])
25+
26+
cities = ['Jakarta','Delhi','Newyork','Bonn','Kolkata','Bangalore','Seoul']
27+
cities_len = {}
28+
for city in cities:
29+
clen = len(city)
30+
# First create entry
31+
if clen not in cities_len:
32+
cities_len[clen] = []
33+
cities_len[clen].append(city)
34+
35+
print('Cities grouped by length=>',cities_len)
36+
37+
# Implementation using default dict
38+
# 1. Counts
39+
counts = defaultdict(int)
40+
for word in text.split():
41+
word = word.lower().strip()
42+
# Value is set to 0 and incremented by 1 in one go
43+
counts[word] += 1
44+
45+
print("Counts of word",word,'=>',counts[word])
46+
47+
# 2. Cities grouped by length
48+
cities = ['Jakarta','Delhi','Newyork','Bonn','Kolkata','Bangalore','Seoul']
49+
cities_len = defaultdict(list)
50+
51+
for city in cities:
52+
# Empty list is created as value and appended to in one go
53+
cities_len[len(city)].append(city)
54+
55+
56+
print('Cities grouped by length=>',cities_len)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Code Listing #9
2+
3+
"""
4+
5+
Return top 10 most common words from the online text of the "The Hound of Baskervilles"
6+
7+
"""
8+
9+
import requests, operator
10+
from collections import defaultdict, Counter
11+
12+
print('Using defaultdict')
13+
text=requests.get('https://www.gutenberg.org/files/2852/2852-0.txt').text
14+
freq=defaultdict(int)
15+
16+
for word in text.split():
17+
if len(word.strip())==0: continue
18+
freq[word.lower()] += 1
19+
20+
print(sorted(freq.items(), key=operator.itemgetter(1), reverse=True)[:10])
21+
22+
print('Using Counter')
23+
freq=Counter(filter(None, map(lambda x:x.lower().strip(), text.split())))
24+
print(freq.most_common(10))
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Code Listing #4
2+
"""
3+
4+
Memory profiler example
5+
6+
"""
7+
8+
@profile
9+
def squares(n):
10+
return [x*x for x in range(1, n+1)]
11+
12+
squares(1000000)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Code Listing #11
2+
3+
"""
4+
5+
Example of using namedtuple
6+
7+
"""
8+
9+
from collections import namedtuple
10+
11+
Employee = namedtuple('Employee', 'name, age, gender, title, department')
12+
print(Employee)
13+
# Create an employee
14+
jack = Employee('Jack',25,'M','Programmer','Engineering')
15+
print(jack)
16+
17+
for field in jack:
18+
print(field)
19+
20+
# This will raise an error
21+
# jack.age=32
22+
23+
# This works fine
24+
print(jack._replace(age=32))
25+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Code Listing #6
2+
3+
"""
4+
5+
Objgraph example code.
6+
7+
Make sure you install graphviz and xdot before running this.
8+
9+
"""
10+
11+
import objgraph
12+
13+
class MyRefClass(object):
14+
pass
15+
16+
ref=MyRefClass()
17+
class C(object):pass
18+
19+
c_objects=[]
20+
for i in range(100):
21+
c=C()
22+
c.ref=ref
23+
c_objects.append(c)
24+
25+
import pdb; pdb.set_trace()
26+
27+
# Run this code in pdb prompt, you just need to press 'c'
28+
objgraph.show_backrefs(ref, max_depth=2, too_many=2, filename='refs.png')
29+
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Code Listing #8
2+
3+
"""
4+
5+
Examples of using OrderedDict
6+
7+
"""
8+
9+
from collections import OrderedDict
10+
11+
cities = ['Jakarta','Delhi','Newyork','Bonn','Kolkata','Bangalore','Seoul']
12+
# Dictionary
13+
cities_dict = dict.fromkeys(cities)
14+
print(cities_dict)
15+
# Ordered dictionary
16+
ocities_dict = OrderedDict.fromkeys(cities)
17+
print(ocities_dict)
18+
19+
# Dropping duplicates while preserving order with Ordered dictionary
20+
cities = ['Jakarta','Delhi','Newyork','Bonn','Kolkata','Bangalore','Bonn','Seoul','Delhi','Jakarta','Mumbai']
21+
cities_odict = OrderedDict.fromkeys(cities)
22+
print(cities_odict.keys())
23+
print(cities_odict.popitem())
24+
print(cities_odict.popitem(last=False))
25+
26+
# class LRU
27+
class LRU(OrderedDict):
28+
""" Least recently used cache (LRU) dictionary
29+
using OrderedDict
30+
31+
"""
32+
33+
def __init__(self, size=10):
34+
self.size = size
35+
36+
def set(self, key):
37+
# If key is there delete and reinsert so
38+
# it moves to end.
39+
if key in self:
40+
del self[key]
41+
42+
self[key] = 1
43+
if len(self)>self.size:
44+
# Pop from left
45+
self.popitem(last=False)
46+
47+
if __name__ == "__main__":
48+
d=LRU(size=5)
49+
d.set('bangalore')
50+
d.set('chennai')
51+
d.set('mumbai')
52+
# Bangalore is set again - moves to right
53+
d.set('bangalore')
54+
d.set('kolkata')
55+
d.set('delhi')
56+
# Chennai is set again - so moves to right
57+
d.set('chennai')
58+
59+
print('Length=>',len(d))
60+
print(d)
61+
# Kochi is appended and mumbai drops off
62+
d.set('kochi')
63+
print(d)

0 commit comments

Comments
 (0)