-
Notifications
You must be signed in to change notification settings - Fork 1
/
15tester.py
36 lines (33 loc) · 936 Bytes
/
15tester.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
def tryexcept():
count = {}
with open("15tester_data.txt", "r") as f:
for l in f:
for c in l:
try:
count[c] += 1
except KeyError:
count[c] = 1
return count
def dictget():
count = {}
with open("15tester_data.txt", "r") as f:
for l in f:
for c in l:
count[c] = count.get(c,0) + 1
return count
def default():
from collections import defaultdict
count = defaultdict(int)
with open("15tester_data.txt", "r") as f:
for l in f:
for c in l:
count[c] += 1
return count
if __name__ == "__main__":
from timeit import Timer
t1 = Timer(tryexcept)
t2 = Timer(dictget)
t3 = Timer(default)
print "try ... except ... : ", t1.timeit(10000)
print "dict.get : ", t2.timeit(10000)
print "defaultdict : ", t3.timeit(10000)