Skip to content

Commit a01213b

Browse files
bites 62
1 parent e64e580 commit a01213b

File tree

3 files changed

+131
-0
lines changed

3 files changed

+131
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,4 @@
4141
/3/README.md
4242
/36/README.md
4343
/10/README.md
44+
/62/README.md

62/performance.py

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import collections
2+
from functools import wraps
3+
from time import time
4+
from typing import Deque, List, Set, Generator
5+
6+
7+
def timing(f):
8+
"""A simple timer decorator to print the elapsed time of
9+
the execution of the function it wraps.
10+
Returns (timing, result) tuple"""
11+
@wraps(f)
12+
def wrapper(*args, **kwargs):
13+
start = time()
14+
result = f(*args, **kwargs)
15+
end = time()
16+
duration = end - start
17+
print(f'Elapsed time {f.__name__}: {duration}')
18+
return duration, result
19+
return wrapper
20+
21+
22+
@timing
23+
def contains(sequence: List[int], num: int) -> bool:
24+
for n in sequence:
25+
if n == num:
26+
return True
27+
return False
28+
29+
30+
@timing
31+
def contains_fast(sequence: Set[int], num: int) -> bool:
32+
return sequence.__contains__(num)
33+
34+
35+
@timing
36+
def ordered_list_max(sequence: List[int]) -> int:
37+
return max(sequence)
38+
39+
40+
@timing
41+
def ordered_list_max_fast(sequence: List[int]) -> int:
42+
sortedsequence = sorted(sequence)
43+
return sortedsequence[-1]
44+
45+
@timing
46+
def list_concat(sequence: List[str]) -> str:
47+
bigstr = ''
48+
for i in sequence:
49+
bigstr += str(i)
50+
return bigstr
51+
52+
53+
@timing
54+
def list_concat_fast(sequence: List[str]) -> str:
55+
return ''.join(sequence)
56+
57+
58+
@timing
59+
def list_inserts(n: int) -> List[int]:
60+
lst = []
61+
for i in range(n):
62+
lst.insert(0, i)
63+
return lst
64+
65+
66+
@timing
67+
def list_inserts_fast(n: int) -> Deque[int]:
68+
deQ = collections.deque([])
69+
for i in range(n):
70+
deQ.appendleft(i)
71+
return deQ
72+
73+
74+
@timing
75+
def list_creation(n: int) -> List[int]:
76+
lst = []
77+
for i in range(n):
78+
lst.append(i)
79+
return lst
80+
81+
82+
@timing
83+
def list_creation_fast(n: int) -> List[int]:
84+
return (x for x in range(n))

62/test_performance.py

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
from string import ascii_lowercase
2+
3+
from performance import (contains, contains_fast,
4+
ordered_list_max, ordered_list_max_fast,
5+
list_concat, list_concat_fast,
6+
list_inserts, list_inserts_fast,
7+
list_creation, list_creation_fast)
8+
9+
alist = list(range(1000000))
10+
aset = set(alist)
11+
listofstrings = list(ascii_lowercase) * 1000
12+
13+
14+
def test_contains():
15+
t1, res1 = contains(alist, 500)
16+
t2, res2 = contains_fast(aset, 1000)
17+
assert res1 == res2
18+
assert t1 > t2
19+
20+
21+
def test_ordered_max():
22+
t1, res1 = ordered_list_max(alist)
23+
t2, res2 = ordered_list_max_fast(alist)
24+
assert res1 == res2
25+
assert t1 > t2
26+
27+
28+
def test_concat():
29+
t1, res1 = list_concat(listofstrings)
30+
t2, res2 = list_concat_fast(listofstrings)
31+
assert res1 == res2
32+
assert t1 > t2
33+
34+
35+
def test_list_insert():
36+
t1, res1 = list_inserts(10000)
37+
t2, res2 = list_inserts_fast(10000)
38+
assert list(res1) == list(res2)
39+
assert t1 > t2
40+
41+
42+
def test_list_creation():
43+
t1, res1 = list_creation(10000)
44+
t2, res2 = list_creation_fast(10000)
45+
assert list(res1) == list(res2)
46+
assert t1 > t2

0 commit comments

Comments
 (0)