# Untitled

a guest
Dec 1st, 2017
173
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. from timeit import timeit
2. from collections import Counter
3. import types
4. import random
5.
6. def setup_data(n):
7.     digits = "0123456789"
8.     return dict(text = ''.join(random.choice(digits) for i in range(n)))
9.
10.
11. def f_counter(text):
12.     c = Counter()
13.     for i in range(len(text)-2):
14.         ss = text[i:i+3]
15.         c.update([ss])
16.     return (i for i in c.items() if i[1] > 1)
17.
18. def f_counter2(text):
19.     c = Counter()
20.     for i in range(len(text)-2):
21.         ss = text[i:i+3]
22.         c[ss] += 1
23.     return (i for i in c.items() if i[1] > 1)
24.
25. def f_counter3(text):
26.     c = Counter(text[i:i+3] for i in range(len(text) - 2))
27.     return (i for i in c.items() if i[1] > 1)
28.
29. def f_dict(text):
30.     d = {}
31.     for i in range(len(text)-2):
32.         ss = text[i:i+3]
33.         if ss not in d:
34.             d[ss] = 0
35.         d[ss] += 1
36.     return ((i, d[i]) for i in d if d[i] > 1)
37.
38. def f_array(text):
39.     a = [[[0 for _ in range(10)] for _ in range(10)] for _ in range(10)]
40.     for n in range(len(text)-2):
41.         i, j, k = (int(ss) for ss in text[n:n+3])
42.         a[i][j][k] += 1
43.     for i, b in enumerate(a):
44.         for j, c in enumerate(b):
45.             for k, d in enumerate(c):
46.                 if d > 1: yield (f'{i}{j}{k}', d)
47.
48.
49. for n in (1E1, 1E3, 1E5):
50.     n = int(n)
51.     data = setup_data(n)
52.     print(f'n = {n}')
53.     results = {}
54.     for name, func in list(globals().items()):
55.         if not name.startswith('f_') or not isinstance(func, types.FunctionType):
56.             continue
57.         print("{:16s}{:16.8f} ms".format(name[2:], timeit(
58.             'results[name] = list(f(**data))', globals={'f':func, 'data':data, 'results':results, 'name':name}, number=10)*100))
59.     for r in results:
60.         print('{:10}: {}'.format(r, sorted(results[r])[:5]))
61.
62. # n = 10
63. # counter               0.06956750 ms
64. # counter2              0.02106030 ms
65. # counter3              0.03124950 ms
66. # dict                  0.00937660 ms
67. # array                 0.27001170 ms
68. # f_counter : []
69. # f_counter2: []
70. # f_counter3: []
71. # f_dict    : []
72. # f_array   : []
73. # n = 1000
74. # counter               5.18519850 ms
75. # counter2              1.51690750 ms
76. # counter3              0.57832220 ms
77. # dict                  0.66203990 ms
78. # array                 2.86603400 ms
79. # f_counter : [('002', 2), ('006', 3), ('012', 2), ('014', 2), ('017', 2)]
80. # f_counter2: [('002', 2), ('006', 3), ('012', 2), ('014', 2), ('017', 2)]
81. # f_counter3: [('002', 2), ('006', 3), ('012', 2), ('014', 2), ('017', 2)]
82. # f_dict    : [('002', 2), ('006', 3), ('012', 2), ('014', 2), ('017', 2)]
83. # f_array   : [('002', 2), ('006', 3), ('012', 2), ('014', 2), ('017', 2)]
84. # n = 100000
85. # counter             516.46002940 ms
86. # counter2             78.18013590 ms
87. # counter3             42.89571540 ms
88. # dict                 46.40053570 ms
89. # array               241.30035320 ms
90. # f_counter : [('000', 97), ('001', 116), ('002', 103), ('003', 102), ('004', 94)]
91. # f_counter2: [('000', 97), ('001', 116), ('002', 103), ('003', 102), ('004', 94)]
92. # f_counter3: [('000', 97), ('001', 116), ('002', 103), ('003', 102), ('004', 94)]
93. # f_dict    : [('000', 97), ('001', 116), ('002', 103), ('003', 102), ('004', 94)]
94. # f_array   : [('000', 97), ('001', 116), ('002', 103), ('003', 102), ('004', 94)]
RAW Paste Data